|
| 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/pmezard/go-difflib/difflib" |
| 23 | + appsv1 "k8s.io/api/apps/v1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + rbacv1 "k8s.io/api/rbac/v1" |
| 26 | + "k8s.io/apimachinery/pkg/api/equality" |
| 27 | + "sigs.k8s.io/yaml" |
| 28 | +) |
| 29 | + |
| 30 | +// deploymentNeedsUpdate checks if a deployment needs to be updated using K8s semantic equality. |
| 31 | +func deploymentNeedsUpdate(existing, desired *appsv1.Deployment) bool { |
| 32 | + // Compare labels (only if desired.Labels is non-nil) |
| 33 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 34 | + return true |
| 35 | + } |
| 36 | + |
| 37 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 38 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 39 | + return true |
| 40 | + } |
| 41 | + |
| 42 | + // Compare the entire Spec using K8s semantic equality (handles nil vs empty automatically) |
| 43 | + return !equality.Semantic.DeepEqual(existing.Spec, desired.Spec) |
| 44 | +} |
| 45 | + |
| 46 | +// serviceAccountNeedsUpdate checks if a service account needs to be updated using K8s semantic equality. |
| 47 | +func serviceAccountNeedsUpdate(existing, desired *corev1.ServiceAccount) bool { |
| 48 | + // Compare labels (only if desired.Labels is non-nil) |
| 49 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 50 | + return true |
| 51 | + } |
| 52 | + |
| 53 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 54 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 55 | + return true |
| 56 | + } |
| 57 | + |
| 58 | + return false |
| 59 | +} |
| 60 | + |
| 61 | +// roleNeedsUpdate checks if a role needs to be updated using K8s semantic equality. |
| 62 | +func roleNeedsUpdate(existing, desired *rbacv1.Role) bool { |
| 63 | + // Compare labels (only if desired.Labels is non-nil) |
| 64 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 65 | + return true |
| 66 | + } |
| 67 | + |
| 68 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 69 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 70 | + return true |
| 71 | + } |
| 72 | + |
| 73 | + // Compare rules (only if non-nil in desired) |
| 74 | + if desired.Rules != nil && !equality.Semantic.DeepEqual(existing.Rules, desired.Rules) { |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +// roleBindingNeedsUpdate checks if a role binding needs to be updated using K8s semantic equality. |
| 82 | +func roleBindingNeedsUpdate(existing, desired *rbacv1.RoleBinding) bool { |
| 83 | + // Compare labels (only if desired.Labels is non-nil) |
| 84 | + if desired.Labels != nil && !equality.Semantic.DeepEqual(existing.Labels, desired.Labels) { |
| 85 | + return true |
| 86 | + } |
| 87 | + |
| 88 | + // Compare annotations (only if desired.Annotations is non-nil) |
| 89 | + if desired.Annotations != nil && !equality.Semantic.DeepEqual(existing.Annotations, desired.Annotations) { |
| 90 | + return true |
| 91 | + } |
| 92 | + |
| 93 | + // Compare subjects (only if non-nil in desired) |
| 94 | + if desired.Subjects != nil && !equality.Semantic.DeepEqual(existing.Subjects, desired.Subjects) { |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + // Compare role ref (only if non-zero in desired) |
| 99 | + if desired.RoleRef.Name != "" && !equality.Semantic.DeepEqual(existing.RoleRef, desired.RoleRef) { |
| 100 | + return true |
| 101 | + } |
| 102 | + |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +// generateDiff creates a unified diff between existing and desired resources. |
| 107 | +// It works with any Kubernetes resource type. |
| 108 | +// Returns the diff string and any error encountered during serialization. |
| 109 | +func generateDiff[T any](existing, desired *T) (string, error) { |
| 110 | + // Serialize existing resource to YAML |
| 111 | + existingYAML, err := yaml.Marshal(existing) |
| 112 | + if err != nil { |
| 113 | + return "", fmt.Errorf("failed to marshal existing resource: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Serialize desired resource to YAML |
| 117 | + desiredYAML, err := yaml.Marshal(desired) |
| 118 | + if err != nil { |
| 119 | + return "", fmt.Errorf("failed to marshal desired resource: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + // Generate unified diff |
| 123 | + diff := difflib.UnifiedDiff{ |
| 124 | + A: difflib.SplitLines(string(existingYAML)), |
| 125 | + B: difflib.SplitLines(string(desiredYAML)), |
| 126 | + FromFile: "Existing", |
| 127 | + ToFile: "Desired", |
| 128 | + Context: 3, |
| 129 | + } |
| 130 | + |
| 131 | + return difflib.GetUnifiedDiffString(diff) |
| 132 | +} |
0 commit comments