|
| 1 | +// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package chi |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/golang/glog" |
| 19 | + "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | +) |
| 21 | + |
| 22 | +func (c *Controller) labelMyObjectsTree() { |
| 23 | + |
| 24 | + // Operator is running in the Pod. Label Pod |
| 25 | + // Pod is owned by ReplicaSet. Label ReplicaSet also. |
| 26 | + // ReplicaSet is owned by Deployment. Label Deployment also. |
| 27 | + // Deployment is not owned so far. |
| 28 | + // |
| 29 | + // Excerpt from Pod's yaml |
| 30 | + // metadata: |
| 31 | + // ownerReferences: |
| 32 | + // - apiVersion: apps/v1 |
| 33 | + // blockOwnerDeletion: true |
| 34 | + // controller: true |
| 35 | + // kind: ReplicaSet |
| 36 | + // name: clickhouse-operator-79bf98f9b8 |
| 37 | + // uid: a276f30c-83ae-11e9-b92d-0208b778ea1a |
| 38 | + // |
| 39 | + // Excerpt from ReplicaSet's yaml |
| 40 | + // metadata: |
| 41 | + // ownerReferences: |
| 42 | + // - apiVersion: apps/v1 |
| 43 | + // blockOwnerDeletion: true |
| 44 | + // controller: true |
| 45 | + // kind: Deployment |
| 46 | + // name: clickhouse-operator |
| 47 | + // uid: a275a8a0-83ae-11e9-b92d-0208b778ea1a |
| 48 | + |
| 49 | + // Label operator's Pod with version label |
| 50 | + podName, ok1 := c.runtimeParams["OPERATOR_POD_NAME"] |
| 51 | + namespace, ok2 := c.runtimeParams["OPERATOR_POD_NAMESPACE"] |
| 52 | + |
| 53 | + if !ok1 || !ok2 { |
| 54 | + glog.V(1).Info("ERROR fetch Pod name out of %s/%s", namespace, podName) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // Pod namespaced name found, fetch it |
| 59 | + pod, err := c.podLister.Pods(namespace).Get(podName) |
| 60 | + if err != nil { |
| 61 | + glog.V(1).Info("ERROR get Pod %s/%s", namespace, podName) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // Put label on the Pod |
| 66 | + pod.Labels["version"] = c.version |
| 67 | + if _, err := c.kubeClient.CoreV1().Pods(namespace).Update(pod); err != nil { |
| 68 | + glog.V(1).Info("ERROR put label on Pod %s/%s", namespace, podName) |
| 69 | + } |
| 70 | + |
| 71 | + // Find parent ReplicaSet |
| 72 | + replicaSetName := "" |
| 73 | + for i := range pod.OwnerReferences { |
| 74 | + owner := &pod.OwnerReferences[i] |
| 75 | + if owner.Kind == "ReplicaSet" { |
| 76 | + // ReplicaSet found |
| 77 | + replicaSetName = owner.Name |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if replicaSetName == "" { |
| 83 | + // ReplicaSet not found |
| 84 | + glog.V(1).Info("ERROR ReplicaSet for Pod %s/%s not found", namespace, podName) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // ReplicaSet namespaced name found, fetch it |
| 89 | + replicaSet, err := c.kubeClient.AppsV1().ReplicaSets(namespace).Get(replicaSetName, v1.GetOptions{}) |
| 90 | + if err != nil { |
| 91 | + glog.V(1).Info("ERROR get ReplicaSet %s/%s", namespace, replicaSetName) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + // Put label on the ReplicaSet |
| 96 | + replicaSet.Labels["version"] = c.version |
| 97 | + if _, err := c.kubeClient.AppsV1().ReplicaSets(namespace).Update(replicaSet); err != nil { |
| 98 | + glog.V(1).Info("ERROR put label on ReplicaSet %s/%s", namespace, replicaSetName) |
| 99 | + } |
| 100 | + |
| 101 | + // Find parent Deployment |
| 102 | + deploymentName := "" |
| 103 | + for i := range replicaSet.OwnerReferences { |
| 104 | + owner := &replicaSet.OwnerReferences[i] |
| 105 | + if owner.Kind == "Deployment" { |
| 106 | + // Deployment found |
| 107 | + deploymentName = owner.Name |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if deploymentName == "" { |
| 113 | + // Deployment not found |
| 114 | + glog.V(1).Info("ERROR Deployment for %s Pod %s ReplicaSet %s not found", namespace, podName, replicaSetName) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + // Deployment namespaced name found, fetch it |
| 119 | + deployment, err := c.kubeClient.AppsV1().Deployments(namespace).Get(deploymentName, v1.GetOptions{}) |
| 120 | + if err != nil { |
| 121 | + glog.V(1).Info("ERROR get Deployment %s/%s", namespace, deploymentName) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + // Put label on the Deployment |
| 126 | + deployment.Labels["version"] = c.version |
| 127 | + if _, err := c.kubeClient.AppsV1().Deployments(namespace).Update(deployment); err != nil { |
| 128 | + glog.V(1).Info("ERROR put label on Deployment %s/%s", namespace, deploymentName) |
| 129 | + } |
| 130 | +} |
0 commit comments