|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package jumpstarter |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + "github.com/pmezard/go-difflib/difflib" |
| 24 | + appsv1 "k8s.io/api/apps/v1" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + rbacv1 "k8s.io/api/rbac/v1" |
| 27 | + "k8s.io/apimachinery/pkg/api/equality" |
| 28 | + "sigs.k8s.io/yaml" |
| 29 | +) |
| 30 | + |
| 31 | +// deploymentNeedsUpdate checks if a deployment needs to be updated using K8s semantic equality. |
| 32 | +func deploymentNeedsUpdate(existing, desired *appsv1.Deployment) bool { |
| 33 | + // Compare labels (only if desired.Labels is non-nil) |
| 34 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 35 | + return true |
| 36 | + } |
| 37 | + |
| 38 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 39 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 40 | + return true |
| 41 | + } |
| 42 | + |
| 43 | + // Compare the entire Spec using K8s semantic equality (handles nil vs empty automatically) |
| 44 | + return !equality.Semantic.DeepEqual(existing.Spec, desired.Spec) |
| 45 | +} |
| 46 | + |
| 47 | +// configMapNeedsUpdate checks if a configmap needs to be updated using K8s semantic equality. |
| 48 | +func configMapNeedsUpdate(existing, desired *corev1.ConfigMap, log logr.Logger) bool { |
| 49 | + // Compare labels (only if desired.Labels is non-nil) |
| 50 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 51 | + return true |
| 52 | + } |
| 53 | + |
| 54 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 55 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + // Compare data (only if desired.Data is non-nil) |
| 60 | + if desired.Data != nil && !equality.Semantic.DeepEqual(existing.Data, desired.Data) { |
| 61 | + return true |
| 62 | + } |
| 63 | + |
| 64 | + // Compare binary data (only if desired.BinaryData is non-nil) |
| 65 | + if desired.BinaryData != nil && !equality.Semantic.DeepEqual(existing.BinaryData, desired.BinaryData) { |
| 66 | + return true |
| 67 | + } |
| 68 | + |
| 69 | + return false |
| 70 | +} |
| 71 | + |
| 72 | +// serviceAccountNeedsUpdate checks if a service account needs to be updated using K8s semantic equality. |
| 73 | +func serviceAccountNeedsUpdate(existing, desired *corev1.ServiceAccount) bool { |
| 74 | + // Compare labels (only if desired.Labels is non-nil) |
| 75 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 80 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 81 | + return true |
| 82 | + } |
| 83 | + |
| 84 | + return false |
| 85 | +} |
| 86 | + |
| 87 | +// roleNeedsUpdate checks if a role needs to be updated using K8s semantic equality. |
| 88 | +func roleNeedsUpdate(existing, desired *rbacv1.Role) bool { |
| 89 | + // Compare labels (only if desired.Labels is non-nil) |
| 90 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 91 | + return true |
| 92 | + } |
| 93 | + |
| 94 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 95 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 96 | + return true |
| 97 | + } |
| 98 | + |
| 99 | + // Compare rules (only if non-nil in desired) |
| 100 | + if desired.Rules != nil && !equality.Semantic.DeepEqual(existing.Rules, desired.Rules) { |
| 101 | + return true |
| 102 | + } |
| 103 | + |
| 104 | + return false |
| 105 | +} |
| 106 | + |
| 107 | +// roleBindingNeedsUpdate checks if a role binding needs to be updated using K8s semantic equality. |
| 108 | +func roleBindingNeedsUpdate(existing, desired *rbacv1.RoleBinding) bool { |
| 109 | + // Compare labels (only if desired.Labels is non-nil) |
| 110 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 111 | + return true |
| 112 | + } |
| 113 | + |
| 114 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 115 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 116 | + return true |
| 117 | + } |
| 118 | + |
| 119 | + // Compare subjects (only if non-nil in desired) |
| 120 | + if desired.Subjects != nil && !equality.Semantic.DeepEqual(existing.Subjects, desired.Subjects) { |
| 121 | + return true |
| 122 | + } |
| 123 | + |
| 124 | + // Compare role ref (only if non-zero in desired) |
| 125 | + if desired.RoleRef.Name != "" && !equality.Semantic.DeepEqual(existing.RoleRef, desired.RoleRef) { |
| 126 | + return true |
| 127 | + } |
| 128 | + |
| 129 | + return false |
| 130 | +} |
| 131 | + |
| 132 | +// generateDiff creates a unified diff between existing and desired resources. |
| 133 | +// It works with any Kubernetes resource type. |
| 134 | +// Returns the diff string and any error encountered during serialization. |
| 135 | +func generateDiff[T any](existing, desired *T) (string, error) { |
| 136 | + // Serialize existing resource to YAML |
| 137 | + existingYAML, err := yaml.Marshal(existing) |
| 138 | + if err != nil { |
| 139 | + return "", fmt.Errorf("failed to marshal existing resource: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + // Serialize desired resource to YAML |
| 143 | + desiredYAML, err := yaml.Marshal(desired) |
| 144 | + if err != nil { |
| 145 | + return "", fmt.Errorf("failed to marshal desired resource: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + // Generate unified diff |
| 149 | + diff := difflib.UnifiedDiff{ |
| 150 | + A: difflib.SplitLines(string(existingYAML)), |
| 151 | + B: difflib.SplitLines(string(desiredYAML)), |
| 152 | + FromFile: "Existing", |
| 153 | + ToFile: "Desired", |
| 154 | + Context: 3, |
| 155 | + } |
| 156 | + |
| 157 | + return difflib.GetUnifiedDiffString(diff) |
| 158 | +} |
0 commit comments