Skip to content

Commit

Permalink
pr: log instead of failure, align test with new error
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Oct 17, 2023
1 parent 3da7474 commit cb16248
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
7 changes: 5 additions & 2 deletions internal/dataplane/kongstate/kongstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (ks *KongState) SanitizedCopy() *KongState {
}

func (ks *KongState) FillConsumersAndCredentials(
logger logr.Logger,
s store.Storer,
failuresCollector *failures.ResourceFailuresCollector,
) {
Expand Down Expand Up @@ -105,8 +106,10 @@ func (ks *KongState) FillConsumersAndCredentials(
// try the label first. if it's present, no need to check the field
credType, credTypeSource := util.ExtractKongCredentialType(secret)
if credTypeSource == util.CredentialTypeFromField {
failuresCollector.PushResourceFailure("credential only has deprecated kongCredType field, needs "+
"konghq.com/credential label", secret)
logger.Error(nil,
fmt.Sprintf("Secret uses deprecated kongCredType field, needs konghq.com/credential=%s label", credType),
"namesapce", secret.Namespace, "name", secret.Name)

}
if !credentials.SupportedTypes.Has(credType) {
pushCredentialResourceFailures(
Expand Down
6 changes: 3 additions & 3 deletions internal/dataplane/kongstate/kongstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func TestFillConsumersAndCredentials(t *testing.T) {
},
},
expectedTranslationFailureMessages: map[k8stypes.NamespacedName]string{
{Namespace: "default", Name: "foo"}: fmt.Sprintf("failed to provision credential: unsupported kongCredType: %q", "unsupported"),
{Namespace: "default", Name: "foo"}: fmt.Sprintf("failed to provision credential: unsupported credential type: %q", "unsupported"),
},
},
{
Expand Down Expand Up @@ -680,7 +680,7 @@ func TestFillConsumersAndCredentials(t *testing.T) {
},
},
expectedTranslationFailureMessages: map[k8stypes.NamespacedName]string{
{Namespace: "default", Name: "foo"}: fmt.Sprintf("failed to provision credential: unsupported kongCredType: %q", "bee-auth"),
{Namespace: "default", Name: "foo"}: fmt.Sprintf("failed to provision credential: unsupported credential type: %q", "bee-auth"),
},
},
{
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestFillConsumersAndCredentials(t *testing.T) {
failureCollector := failures.NewResourceFailuresCollector(logger)

state := KongState{}
state.FillConsumersAndCredentials(store, failureCollector)
state.FillConsumersAndCredentials(logger, store, failureCollector)
// compare translated consumers.
require.Len(t, state.Consumers, len(tc.expectedKongStateConsumers))
// compare fields. Since we only test for translating a single consumer, we only compare the first one if exists.
Expand Down
2 changes: 1 addition & 1 deletion internal/dataplane/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (p *Parser) BuildKongConfig() KongConfigBuildingResult {
result.FillOverrides(p.logger, p.storer)

// generate consumers and credentials
result.FillConsumersAndCredentials(p.storer, p.failuresCollector)
result.FillConsumersAndCredentials(p.logger, p.storer, p.failuresCollector)
for i := range result.Consumers {
p.registerSuccessfullyParsedObject(&result.Consumers[i].K8sKongConsumer)
}
Expand Down
11 changes: 7 additions & 4 deletions internal/util/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (

// TODO https:/Kong/kubernetes-ingress-controller/issues/4853 remove field handling when no longer supported.

// CredentialTypeSource indicates the source of credential type information (or lack thereof) in a Secret.
type CredentialTypeSource int

const (
// CredentialTypeAbsent indicates that no credential information is present in a Secret.
CredentialTypeAbsent = iota
CredentialTypeAbsent CredentialTypeSource = iota
// CredentialTypeFromLabel indicates that a Secret's credential type was determined from a label.
CredentialTypeFromLabel = iota
CredentialTypeFromLabel
// CredentialTypeFromField indicates that a Secret's credential type was determined from a data field.
CredentialTypeFromField = iota
CredentialTypeFromField
)

// ExtractKongCredentialType returns the credential type of a Secret and a code indicating whether the credential type
// was obtained from a label, field, or not at all. Labels take precedence over fields if both are present.
func ExtractKongCredentialType(secret *corev1.Secret) (string, int) {
func ExtractKongCredentialType(secret *corev1.Secret) (string, CredentialTypeSource) {
credType, labelOk := secret.Labels[labels.LabelPrefix+labels.CredentialKey]
if !labelOk {
// if no label, fall back to the deprecated field
Expand Down
2 changes: 1 addition & 1 deletion internal/util/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestExtractKongCredentialType(t *testing.T) {
name string
secret *corev1.Secret
credType string
credTypeSource int
credTypeSource CredentialTypeSource
}{
{
name: "labeled credential",
Expand Down

0 comments on commit cb16248

Please sign in to comment.