-
Notifications
You must be signed in to change notification settings - Fork 304
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
Conversation
d028041
to
2cb76a1
Compare
pkg/backendconfig/validation.go
Outdated
return err | ||
} | ||
} | ||
return nil |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 :)
pkg/backendconfig/validation.go
Outdated
return nil | ||
} | ||
// If necessary, get the OAuth credentials stored in the K8s secret. | ||
if beConfig.Spec.Iap.OAuthClientCredentials.SecretName != "" { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
pkg/backendconfig/validation.go
Outdated
} | ||
clientID, ok := secret.Data[OAuthClientIDKey] | ||
if !ok { | ||
return fmt.Errorf("secret %v has no 'client_id'", secretName) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/backendconfig/validation.go
Outdated
} | ||
clientSecret, ok := secret.Data[OAuthClientSecretKey] | ||
if !ok { | ||
return fmt.Errorf("secret %v has no 'client_secret'", secretName) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/backendconfig/validation.go
Outdated
return fmt.Errorf("secret %v has no 'client_secret'", secretName) | ||
} | ||
beConfig.Spec.Iap.OAuthClientCredentials.ClientID = string(clientID) | ||
beConfig.Spec.Iap.OAuthClientCredentials.ClientSecret = string(clientSecret) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
pkg/backendconfig/validation_test.go
Outdated
{ | ||
"iap and cdn enabled at the same time", | ||
func(kubeClient kubernetes.Interface) { | ||
beConfig.Spec.Cdn = &backendconfigv1beta1.CDNConfig{ |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
pkg/controller/errors/errors.go
Outdated
} | ||
|
||
func (e ErrBackendConfigValidation) Error() string { | ||
return fmt.Sprintf("BackendConfig %v is not valid: %v", e.BackendConfig.Name, e.Err) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
2cb76a1
to
cb8e615
Compare
/lgtm |
cb8e615
to
19a28b8
Compare
/lgtm |
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](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)