Skip to content

Commit

Permalink
Merge pull request #345 from rramkumar1/iap-cdn-changes
Browse files Browse the repository at this point in the history
Modify IAP + CDN support to not touch settings if section in spec is missing
  • Loading branch information
MrHohn authored Jun 18, 2018
2 parents 3521550 + 400ef93 commit af557c2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkg/backends/features/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
// and applies it to the BackendService. It returns true if there were existing
// settings on the BackendService that were overwritten.
func EnsureCDN(sp utils.ServicePort, be *composite.BackendService) bool {
if sp.BackendConfig.Spec.Cdn == nil {
return false
}
beTemp := &composite.BackendService{}
applyCDNSettings(sp, beTemp)
if !reflect.DeepEqual(beTemp.CdnPolicy, be.CdnPolicy) || beTemp.EnableCDN != be.EnableCDN {
Expand Down Expand Up @@ -59,9 +62,6 @@ func applyCDNSettings(sp utils.ServicePort, be *composite.BackendService) {

// setCDNDefaults initializes any nil pointers in CDN configuration which ensures that there are defaults for missing sub-types.
func setCDNDefaults(beConfig *backendconfigv1beta1.BackendConfig) {
if beConfig.Spec.Cdn == nil {
beConfig.Spec.Cdn = &backendconfigv1beta1.CDNConfig{}
}
if beConfig.Spec.Cdn.CachePolicy == nil {
beConfig.Spec.Cdn.CachePolicy = &backendconfigv1beta1.CacheKeyPolicy{}
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/backends/features/cdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ func TestEnsureCDN(t *testing.T) {
be *composite.BackendService
updateExpected bool
}{
{
desc: "cdn setting are missing from spec, no update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: nil,
},
},
},
be: &composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
},
},
},
updateExpected: false,
},
{
desc: "cache policies are missing from spec, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
},
},
},
},
be: &composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
},
},
},
updateExpected: true,
},
{
desc: "settings are identical, no update needed",
sp: utils.ServicePort{
Expand Down
15 changes: 3 additions & 12 deletions pkg/backends/features/iap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

"github.com/golang/glog"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
)
Expand All @@ -30,6 +29,9 @@ import (
// and applies it to the BackendService if it is stale. It returns true
// if there were existing settings on the BackendService that were overwritten.
func EnsureIAP(sp utils.ServicePort, be *composite.BackendService) bool {
if sp.BackendConfig.Spec.Iap == nil {
return false
}
beTemp := &composite.BackendService{}
applyIAPSettings(sp, beTemp)
// We need to compare the SHA256 of the client secret instead of the client secret itself
Expand All @@ -48,20 +50,9 @@ func EnsureIAP(sp utils.ServicePort, be *composite.BackendService) bool {
// made to actually persist the changes.
func applyIAPSettings(sp utils.ServicePort, be *composite.BackendService) {
beConfig := sp.BackendConfig
setIAPDefaults(beConfig)
// Apply the boolean switch
be.Iap = &composite.BackendServiceIAP{Enabled: beConfig.Spec.Iap.Enabled}
// Apply the OAuth credentials
be.Iap.Oauth2ClientId = beConfig.Spec.Iap.OAuthClientCredentials.ClientID
be.Iap.Oauth2ClientSecret = beConfig.Spec.Iap.OAuthClientCredentials.ClientSecret
}

// setIAPDefaults initializes any nil pointers in IAP configuration which ensures that there are defaults for missing sub-types.
func setIAPDefaults(beConfig *backendconfigv1beta1.BackendConfig) {
if beConfig.Spec.Iap == nil {
beConfig.Spec.Iap = &backendconfigv1beta1.IAPConfig{}
}
if beConfig.Spec.Iap.OAuthClientCredentials == nil {
beConfig.Spec.Iap.OAuthClientCredentials = &backendconfigv1beta1.OAuthClientCredentials{}
}
}
18 changes: 18 additions & 0 deletions pkg/backends/features/iap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ func TestEnsureIAP(t *testing.T) {
be *composite.BackendService
updateExpected bool
}{
{
desc: "iap settings are missing from spec, no update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Iap: nil,
},
},
},
be: &composite.BackendService{
Iap: &composite.BackendServiceIAP{
Enabled: true,
Oauth2ClientId: "foo",
Oauth2ClientSecretSha256: fmt.Sprintf("%x", sha256.Sum256([]byte("bar"))),
},
},
updateExpected: false,
},
{
desc: "settings are identical, no update needed",
sp: utils.ServicePort{
Expand Down

0 comments on commit af557c2

Please sign in to comment.