MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / IsServiceAccountAligned

Function IsServiceAccountAligned

pkg/specs/serviceaccount.go:82–128  ·  view source on GitHub ↗

IsServiceAccountAligned compares the given list of pull secrets with the ones managed by the operator inside the given ServiceAccount and returns true when everything is aligned

(
	ctx context.Context,
	sa *corev1.ServiceAccount,
	imagePullSecretsNames []string,
	updatedMetadata metav1.ObjectMeta,
)

Source from the content-addressed store, hash-verified

80// ones managed by the operator inside the given ServiceAccount and returns
81// true when everything is aligned
82func IsServiceAccountAligned(
83 ctx context.Context,
84 sa *corev1.ServiceAccount,
85 imagePullSecretsNames []string,
86 updatedMetadata metav1.ObjectMeta,
87) bool {
88 contextLogger := log.FromContext(ctx)
89 // This is an old version of the ServiceAccount, that need to be refreshed to
90 // store the annotation value
91 if sa.Annotations == nil {
92 return false
93 }
94
95 value := sa.Annotations[utils.OperatorManagedSecretsAnnotationName]
96 if value == "" {
97 return false
98 }
99
100 var serviceAccountPullSecrets []string
101 if err := json.Unmarshal([]byte(value), &serviceAccountPullSecrets); err != nil {
102 contextLogger.Error(err, "Cannot detect if a ServiceAccount need to be refreshed or not, refreshing it",
103 "serviceAccount", sa)
104 return false
105 }
106
107 if len(serviceAccountPullSecrets) != len(imagePullSecretsNames) {
108 return false
109 }
110 if serviceAccountPullSecrets != nil && imagePullSecretsNames != nil && !reflect.DeepEqual(
111 serviceAccountPullSecrets, imagePullSecretsNames) {
112 return false
113 }
114
115 for name, value := range updatedMetadata.Labels {
116 if sa.Labels[name] != value {
117 return false
118 }
119 }
120
121 for name, value := range updatedMetadata.Annotations {
122 if sa.Annotations[name] != value {
123 return false
124 }
125 }
126
127 return true
128}

Callers 2

Calls 1

ErrorMethod · 0.45

Tested by

no test coverage detected