Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom validation for BackendConfig + hook validation into Translator #289

Merged
merged 1 commit into from
May 31, 2018

Conversation

rramkumar1
Copy link
Contributor

@rramkumar1 rramkumar1 commented May 30, 2018

This PR adds custom validation for the BackendConfig, particularly IAP. It also hooks this validation into the existing logic in the translator.

This is the first of a couple upcoming PR's that will provide the plumbing for IAP + CDN support.

/assign @MrHohn @nicksardo


This change is Reviewable

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 30, 2018
@rramkumar1 rramkumar1 force-pushed the iap-cdn-plumbing branch 2 times, most recently from d028041 to 2cb76a1 Compare May 30, 2018 22:26
return err
}
}
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about early return the nil case?

	if beConfig == nil {
		return nil
	}
	return validateIAP(kubeClient, beConfig)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a lot cleaner :)

return nil
}
// If necessary, get the OAuth credentials stored in the K8s secret.
if beConfig.Spec.Iap.OAuthClientCredentials.SecretName != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can beConfig.Spec.Iap.OAuthClientCredentials be nil?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I guess it can not because this field doesn't have omitempty on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, we know it won't be nil based on the CRD validation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: Thinking a nil check would still be good to have --- what if there is a bug in CRD validation that a nil OAuthClientCredentials gets passed in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

}
clientID, ok := secret.Data[OAuthClientIDKey]
if !ok {
return fmt.Errorf("secret %v has no 'client_id'", secretName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace client_id with OAuthClientIDKey?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
clientSecret, ok := secret.Data[OAuthClientSecretKey]
if !ok {
return fmt.Errorf("secret %v has no 'client_secret'", secretName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace client_secret with OAuthClientSecretKey?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return fmt.Errorf("secret %v has no 'client_secret'", secretName)
}
beConfig.Spec.Iap.OAuthClientCredentials.ClientID = string(clientID)
beConfig.Spec.Iap.OAuthClientCredentials.ClientSecret = string(clientSecret)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can user set ClientID and ClientSecret directly in BackendConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if they set it directly and also provide a secret, the data in the secret will overwrite the existing one. If they set it directly but don't provide a secret, we will use what they provided directly.

{
"iap and cdn enabled at the same time",
func(kubeClient kubernetes.Interface) {
beConfig.Spec.Cdn = &backendconfigv1beta1.CDNConfig{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a shared beConfig but modify it in-flight seems error prone...Probably just define a diifrent beConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to tackle this in a separate PR. Left myself a TODO.

backendConfig = backendConfigInStore.DeepCopy()
// Object in cache could be changed in-flight. Deepcopy to
// reduce race conditions.
beConfig = beConfig.DeepCopy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might panic if beConfig is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the DeepCopy() code and it returns nil if the parameter is nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point, yeah it should work.

// reduce race conditions.
beConfig = beConfig.DeepCopy()
if err = backendconfig.Validate(t.ctx.KubeClient, beConfig); err != nil {
return nil, errors.ErrBackendConfigValidation{BackendConfig: *beConfig, Err: err}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check nil before *beConfig as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its nil, then Validate() would not return an error so I dont think we would need that check.

}

func (e ErrBackendConfigValidation) Error() string {
return fmt.Sprintf("BackendConfig %v is not valid: %v", e.BackendConfig.Name, e.Err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include namespace in error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// Object in cache could be changed in-flight. Deepcopy to
// reduce race conditions.
beConfig = beConfig.DeepCopy()
if err = backendconfig.Validate(t.ctx.KubeClient, beConfig); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to do this validation in an earlier stage (e.g. on BackendConfig creation/update)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Humm..Though ingress can still be put into queue triggered by service/ingress update so we need it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, without a webhook, we cannot do it on creation/update.

@MrHohn
Copy link
Member

MrHohn commented May 30, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 30, 2018
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 31, 2018
@MrHohn
Copy link
Member

MrHohn commented May 31, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 31, 2018
@nicksardo nicksardo merged commit 9b1846c into kubernetes:master May 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants