Skip to content

Commit

Permalink
Added check for optional
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Rodan <marcus.rodan@sinch.com>
  • Loading branch information
marcus-rodan-sinch committed Feb 2, 2021
1 parent 56c7708 commit 565b99e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
45 changes: 29 additions & 16 deletions pkg/canary/config_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,29 @@ func (ct *ConfigTracker) GetTargetConfigs(cd *flaggerv1.Canary) (map[string]Conf
return nil, fmt.Errorf("TargetRef.Kind invalid: %s", cd.Spec.TargetRef.Kind)
}

type void struct{}
var member void
secretNames := map[string]void{}
configMapNames := map[string]void{}
secretNames := make(map[string]bool)
configMapNames := make(map[string]bool)

// scan volumes
for _, volume := range vs {
if cmv := volume.ConfigMap; cmv != nil {
name := cmv.Name
configMapNames[name] = member
configMapNames[name] = fieldIsMandatory(cmv.Optional)
}
if sv := volume.Secret; sv != nil {
name := sv.SecretName
secretNames[name] = member
secretNames[name] = fieldIsMandatory(sv.Optional)
}

if projected := volume.Projected; projected != nil {
for _, source := range projected.Sources {
if cmv := source.ConfigMap; cmv != nil {
name := cmv.Name
configMapNames[name] = member
configMapNames[name] = fieldIsMandatory(cmv.Optional)
}
if sv := source.Secret; sv != nil {
name := sv.Name
secretNames[name] = member
secretNames[name] = fieldIsMandatory(sv.Optional)
}
}
}
Expand All @@ -183,10 +181,10 @@ func (ct *ConfigTracker) GetTargetConfigs(cd *flaggerv1.Canary) (map[string]Conf
switch {
case env.ValueFrom.ConfigMapKeyRef != nil:
name := env.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name
configMapNames[name] = member
configMapNames[name] = fieldIsMandatory(env.ValueFrom.ConfigMapKeyRef.Optional)
case env.ValueFrom.SecretKeyRef != nil:
name := env.ValueFrom.SecretKeyRef.LocalObjectReference.Name
secretNames[name] = member
secretNames[name] = fieldIsMandatory(env.ValueFrom.SecretKeyRef.Optional)
}
}
}
Expand All @@ -195,30 +193,38 @@ func (ct *ConfigTracker) GetTargetConfigs(cd *flaggerv1.Canary) (map[string]Conf
switch {
case envFrom.ConfigMapRef != nil:
name := envFrom.ConfigMapRef.LocalObjectReference.Name
configMapNames[name] = member
configMapNames[name] = fieldIsMandatory(envFrom.ConfigMapRef.Optional)
case envFrom.SecretRef != nil:
name := envFrom.SecretRef.LocalObjectReference.Name
secretNames[name] = member
secretNames[name] = fieldIsMandatory(envFrom.SecretRef.Optional)
}
}
}

res := make(map[string]ConfigRef)

for configMapName := range configMapNames {
for configMapName, required := range configMapNames {
config, err := ct.getRefFromConfigMap(configMapName, cd.Namespace)
if err != nil {
return nil, fmt.Errorf("configmap %s.%s get query error: %w", configMapName, cd.Namespace, err)
if required {
return nil, fmt.Errorf("configmap %s.%s get query error: %w", configMapName, cd.Namespace, err)
}
ct.Logger.Errorf("configmap %s.%s get query failed: %w", configMapName, cd.Namespace, err)
continue
}
if config != nil {
res[config.GetName()] = *config
}
}

for secretName := range secretNames {
for secretName, required := range secretNames {
secret, err := ct.getRefFromSecret(secretName, cd.Namespace)
if err != nil {
return nil, fmt.Errorf("secret %s.%s get query error: %w", secretName, cd.Namespace, err)
if required {
return nil, fmt.Errorf("secret %s.%s get query error: %v", secretName, cd.Namespace, err)
}
ct.Logger.Errorf("secret %s.%s get query failed: %v", secretName, cd.Namespace, err)
continue
}
if secret != nil {
res[secret.GetName()] = *secret
Expand All @@ -228,6 +234,13 @@ func (ct *ConfigTracker) GetTargetConfigs(cd *flaggerv1.Canary) (map[string]Conf
return res, nil
}

func fieldIsMandatory(p *bool) bool {
if p == nil {
return false
}
return !*p
}

// GetConfigRefs returns a map of configs and their checksum
func (ct *ConfigTracker) GetConfigRefs(cd *flaggerv1.Canary) (*map[string]string, error) {
res := make(map[string]string)
Expand Down
3 changes: 3 additions & 0 deletions pkg/canary/deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func newDeploymentControllerTestCanary(cc canaryConfigs) *flaggerv1.Canary {
}

func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
var optional bool = false
d := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -478,6 +479,7 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
LocalObjectReference: corev1.LocalObjectReference{
Name: "podinfo-config-vol",
},
Optional: &optional,
},
},
},
Expand All @@ -486,6 +488,7 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "podinfo-secret-vol",
Optional: &optional,
},
},
},
Expand Down

0 comments on commit 565b99e

Please sign in to comment.