From aa9b8453a0225e41df47277175193ef2a5b74e57 Mon Sep 17 00:00:00 2001 From: zhengtianbao Date: Wed, 15 Dec 2021 02:18:19 -0600 Subject: [PATCH] registry: service add clusterIP, nodePort, loadBalancer support (#8291) * registry: service add clusterIP, nodePort, loadBalancer support * modify camelcase name to underscore * Add registry service type compatibility check --- .../registry/defaults/main.yml | 12 +++++++++ roles/kubernetes-apps/registry/tasks/main.yml | 25 +++++++++++++++++++ .../registry/templates/registry-svc.yml.j2 | 15 +++++++++++ 3 files changed, 52 insertions(+) diff --git a/roles/kubernetes-apps/registry/defaults/main.yml b/roles/kubernetes-apps/registry/defaults/main.yml index bcbabfb23..ef9e2fb91 100644 --- a/roles/kubernetes-apps/registry/defaults/main.yml +++ b/roles/kubernetes-apps/registry/defaults/main.yml @@ -5,6 +5,18 @@ registry_storage_access_mode: "ReadWriteOnce" registry_disk_size: "10Gi" registry_port: 5000 registry_replica_count: 1 + +# type of service: ClusterIP, LoadBalancer or NodePort +registry_service_type: "ClusterIP" +# you can specify your cluster IP address when registry_service_type is ClusterIP +registry_service_cluster_ip: "" +# you can specify your cloud provider assigned loadBalancerIP when registry_service_type is LoadBalancer +registry_service_loadbalancer_ip: "" +# annotations for managing Cloud Load Balancers +registry_service_annotations: {} +# you can specify the node port when registry_service_type is NodePort +registry_service_nodeport: "" + # name of kubernetes secret for registry TLS certs registry_tls_secret: "" diff --git a/roles/kubernetes-apps/registry/tasks/main.yml b/roles/kubernetes-apps/registry/tasks/main.yml index b9f0ee432..de462771d 100644 --- a/roles/kubernetes-apps/registry/tasks/main.yml +++ b/roles/kubernetes-apps/registry/tasks/main.yml @@ -1,4 +1,29 @@ --- +- name: Registry | check registry_service_type value + fail: + msg: "registry_service_type can only be 'ClusterIP', 'LoadBalancer' or 'NodePort'" + when: registry_service_type not in ['ClusterIP', 'LoadBalancer', 'NodePort'] + +- name: Registry | Stop if registry_service_cluster_ip is defined when registry_service_type is not 'ClusterIP' + fail: + msg: "registry_service_cluster_ip support only compatible with ClusterIP." + when: + - registry_service_cluster_ip is defined and registry_service_cluster_ip != "" + - registry_service_type != "ClusterIP" + +- name: Registry | Stop if registry_service_loadbalancer_ip is defined when registry_service_type is not 'LoadBalancer' + fail: + msg: "registry_service_loadbalancer_ip support only compatible with LoadBalancer." + when: + - registry_service_loadbalancer_ip is defined and registry_service_loadbalancer_ip != "" + - registry_service_type != "LoadBalancer" + +- name: Registry | Stop if registry_service_nodeport is defined when registry_service_type is not 'NodePort' + fail: + msg: "registry_service_nodeport support only compatible with NodePort." + when: + - registry_service_nodeport is defined and registry_service_nodeport != "" + - registry_service_type != "NodePort" - name: Registry | Create addon dir file: diff --git a/roles/kubernetes-apps/registry/templates/registry-svc.yml.j2 b/roles/kubernetes-apps/registry/templates/registry-svc.yml.j2 index 4ebf8626b..5485aa8aa 100644 --- a/roles/kubernetes-apps/registry/templates/registry-svc.yml.j2 +++ b/roles/kubernetes-apps/registry/templates/registry-svc.yml.j2 @@ -8,10 +8,25 @@ metadata: k8s-app: registry addonmanager.kubernetes.io/mode: Reconcile kubernetes.io/name: "KubeRegistry" +{% if registry_service_annotations %} + annotations: + {{ registry_service_annotations | to_nice_yaml(indent=2, width=1337) | indent(width=4) }} +{% endif %} spec: selector: k8s-app: registry + type: {{ registry_service_type }} +{% if registry_service_type == "ClusterIP" and registry_service_cluster_ip != "" %} + clusterIP: {{ registry_service_cluster_ip }} +{% endif %} +{% if registry_service_type == "LoadBalancer" and registry_service_loadbalancer_ip != "" %} + loadBalancerIP: {{ registry_service_loadbalancer_ip }} +{% endif %} ports: - name: registry port: {{ registry_port }} protocol: TCP + targetPort: {{ registry_port }} +{% if registry_service_type == "NodePort" and registry_service_nodeport != "" %} + nodePort: {{ registry_service_nodeport }} +{% endif %}