|
| 1 | +package reconcile |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/go-logr/logr" |
| 8 | + |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + apierrs "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | +) |
| 16 | + |
| 17 | +// Deployment reconciles a k8s deployment object. |
| 18 | +func Deployment(ctx context.Context, r client.Client, deployment *appsv1.Deployment, log logr.Logger) error { |
| 19 | + foundDeployment := &appsv1.Deployment{} |
| 20 | + justCreated := false |
| 21 | + if err := r.Get(ctx, types.NamespacedName{Name: deployment.Name, Namespace: deployment.Namespace}, foundDeployment); err != nil { |
| 22 | + if apierrs.IsNotFound(err) { |
| 23 | + log.Info("Creating Deployment", "namespace", deployment.Namespace, "name", deployment.Name) |
| 24 | + if err := r.Create(ctx, deployment); err != nil { |
| 25 | + log.Error(err, "unable to create deployment") |
| 26 | + return err |
| 27 | + } |
| 28 | + justCreated = true |
| 29 | + } else { |
| 30 | + log.Error(err, "error getting deployment") |
| 31 | + return err |
| 32 | + } |
| 33 | + } |
| 34 | + if !justCreated && CopyDeploymentSetFields(deployment, foundDeployment) { |
| 35 | + log.Info("Updating Deployment", "namespace", deployment.Namespace, "name", deployment.Name) |
| 36 | + if err := r.Update(ctx, foundDeployment); err != nil { |
| 37 | + log.Error(err, "unable to update deployment") |
| 38 | + return err |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// Service reconciles a k8s service object. |
| 46 | +func Service(ctx context.Context, r client.Client, service *corev1.Service, log logr.Logger) error { |
| 47 | + foundService := &corev1.Service{} |
| 48 | + justCreated := false |
| 49 | + if err := r.Get(ctx, types.NamespacedName{Name: service.Name, Namespace: service.Namespace}, foundService); err != nil { |
| 50 | + if apierrs.IsNotFound(err) { |
| 51 | + log.Info("Creating Service", "namespace", service.Namespace, "name", service.Name) |
| 52 | + if err = r.Create(ctx, service); err != nil { |
| 53 | + log.Error(err, "unable to create service") |
| 54 | + return err |
| 55 | + } |
| 56 | + justCreated = true |
| 57 | + } else { |
| 58 | + log.Error(err, "error getting service") |
| 59 | + return err |
| 60 | + } |
| 61 | + } |
| 62 | + if !justCreated && CopyServiceFields(service, foundService) { |
| 63 | + log.Info("Updating Service\n", "namespace", service.Namespace, "name", service.Name) |
| 64 | + if err := r.Update(ctx, foundService); err != nil { |
| 65 | + log.Error(err, "unable to update Service") |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// VirtualService reconciles an Istio virtual service object. |
| 74 | +func VirtualService(ctx context.Context, r client.Client, virtualServiceName, namespace string, virtualservice *unstructured.Unstructured, log logr.Logger) error { |
| 75 | + foundVirtualService := &unstructured.Unstructured{} |
| 76 | + foundVirtualService.SetAPIVersion("networking.istio.io/v1alpha3") |
| 77 | + foundVirtualService.SetKind("VirtualService") |
| 78 | + justCreated := false |
| 79 | + if err := r.Get(ctx, types.NamespacedName{Name: virtualServiceName, Namespace: namespace}, foundVirtualService); err != nil { |
| 80 | + if apierrs.IsNotFound(err) { |
| 81 | + log.Info("Creating virtual service", "namespace", namespace, "name", virtualServiceName) |
| 82 | + if err := r.Create(ctx, virtualservice); err != nil { |
| 83 | + log.Error(err, "unable to create virtual service") |
| 84 | + return err |
| 85 | + } |
| 86 | + justCreated = true |
| 87 | + } else { |
| 88 | + log.Error(err, "error getting virtual service") |
| 89 | + return err |
| 90 | + } |
| 91 | + } |
| 92 | + if !justCreated && CopyVirtualService(virtualservice, foundVirtualService) { |
| 93 | + log.Info("Updating virtual service", "namespace", namespace, "name", virtualServiceName) |
| 94 | + if err := r.Update(ctx, foundVirtualService); err != nil { |
| 95 | + log.Error(err, "unable to update virtual service") |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// Reference: https://github.com/pwittrock/kubebuilder-workshop/blob/master/pkg/util/util.go |
| 104 | + |
| 105 | +// CopyStatefulSetFields copies the owned fields from one StatefulSet to another |
| 106 | +// Returns true if the fields copied from don't match to. |
| 107 | +func CopyStatefulSetFields(from, to *appsv1.StatefulSet) bool { |
| 108 | + requireUpdate := false |
| 109 | + for k, v := range to.Labels { |
| 110 | + if from.Labels[k] != v { |
| 111 | + requireUpdate = true |
| 112 | + } |
| 113 | + } |
| 114 | + to.Labels = from.Labels |
| 115 | + |
| 116 | + for k, v := range to.Annotations { |
| 117 | + if from.Annotations[k] != v { |
| 118 | + requireUpdate = true |
| 119 | + } |
| 120 | + } |
| 121 | + to.Annotations = from.Annotations |
| 122 | + |
| 123 | + if *from.Spec.Replicas != *to.Spec.Replicas { |
| 124 | + *to.Spec.Replicas = *from.Spec.Replicas |
| 125 | + requireUpdate = true |
| 126 | + } |
| 127 | + |
| 128 | + if !reflect.DeepEqual(to.Spec.Template.Spec, from.Spec.Template.Spec) { |
| 129 | + requireUpdate = true |
| 130 | + } |
| 131 | + to.Spec.Template.Spec = from.Spec.Template.Spec |
| 132 | + |
| 133 | + return requireUpdate |
| 134 | +} |
| 135 | + |
| 136 | +func CopyDeploymentSetFields(from, to *appsv1.Deployment) bool { |
| 137 | + requireUpdate := false |
| 138 | + for k, v := range to.Labels { |
| 139 | + if from.Labels[k] != v { |
| 140 | + requireUpdate = true |
| 141 | + } |
| 142 | + } |
| 143 | + to.Labels = from.Labels |
| 144 | + |
| 145 | + for k, v := range to.Annotations { |
| 146 | + if from.Annotations[k] != v { |
| 147 | + requireUpdate = true |
| 148 | + } |
| 149 | + } |
| 150 | + to.Annotations = from.Annotations |
| 151 | + |
| 152 | + if from.Spec.Replicas != to.Spec.Replicas { |
| 153 | + to.Spec.Replicas = from.Spec.Replicas |
| 154 | + requireUpdate = true |
| 155 | + } |
| 156 | + |
| 157 | + if !reflect.DeepEqual(to.Spec.Template.Spec, from.Spec.Template.Spec) { |
| 158 | + requireUpdate = true |
| 159 | + } |
| 160 | + to.Spec.Template.Spec = from.Spec.Template.Spec |
| 161 | + |
| 162 | + return requireUpdate |
| 163 | +} |
| 164 | + |
| 165 | +// CopyServiceFields copies the owned fields from one Service to another |
| 166 | +func CopyServiceFields(from, to *corev1.Service) bool { |
| 167 | + requireUpdate := false |
| 168 | + for k, v := range to.Labels { |
| 169 | + if from.Labels[k] != v { |
| 170 | + requireUpdate = true |
| 171 | + } |
| 172 | + } |
| 173 | + to.Labels = from.Labels |
| 174 | + |
| 175 | + for k, v := range to.Annotations { |
| 176 | + if from.Annotations[k] != v { |
| 177 | + requireUpdate = true |
| 178 | + } |
| 179 | + } |
| 180 | + to.Annotations = from.Annotations |
| 181 | + |
| 182 | + // Don't copy the entire Spec, because we can't overwrite the clusterIp field |
| 183 | + |
| 184 | + if !reflect.DeepEqual(to.Spec.Selector, from.Spec.Selector) { |
| 185 | + requireUpdate = true |
| 186 | + } |
| 187 | + to.Spec.Selector = from.Spec.Selector |
| 188 | + |
| 189 | + if !reflect.DeepEqual(to.Spec.Ports, from.Spec.Ports) { |
| 190 | + requireUpdate = true |
| 191 | + } |
| 192 | + to.Spec.Ports = from.Spec.Ports |
| 193 | + |
| 194 | + return requireUpdate |
| 195 | +} |
| 196 | + |
| 197 | +// Copy configuration related fields to another instance and returns true if there |
| 198 | +// is a diff and thus needs to update. |
| 199 | +func CopyVirtualService(from, to *unstructured.Unstructured) bool { |
| 200 | + fromSpec, found, err := unstructured.NestedMap(from.Object, "spec") |
| 201 | + if !found { |
| 202 | + return false |
| 203 | + } |
| 204 | + if err != nil { |
| 205 | + return false |
| 206 | + } |
| 207 | + |
| 208 | + toSpec, found, err := unstructured.NestedMap(to.Object, "spec") |
| 209 | + if !found || err != nil { |
| 210 | + unstructured.SetNestedMap(to.Object, fromSpec, "spec") |
| 211 | + return true |
| 212 | + } |
| 213 | + |
| 214 | + requiresUpdate := !reflect.DeepEqual(fromSpec, toSpec) |
| 215 | + if requiresUpdate { |
| 216 | + unstructured.SetNestedMap(to.Object, fromSpec, "spec") |
| 217 | + } |
| 218 | + return requiresUpdate |
| 219 | +} |
0 commit comments