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

OIDC - Support auto generation of PingSource identity service account and expose in AuthStatus #7344

Merged
merged 8 commits into from
Oct 19, 2023
Merged
6 changes: 6 additions & 0 deletions pkg/adapter/mtping/pingsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceDeployed,
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
),
},
WantErr: false,
Expand All @@ -112,6 +113,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceDeployed,
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
),
},
WantErr: false,
Expand All @@ -137,6 +139,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceDeployed,
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
),
},
WantErr: false,
Expand All @@ -162,6 +165,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceDeployed,
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
),
},
WantErr: false,
Expand All @@ -188,6 +192,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceDeleted,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
),
},
WantErr: false,
Expand All @@ -210,6 +215,7 @@ func TestAllCases(t *testing.T) {
rtv1.WithPingSourceSink(sinkAddr),
rtv1.WithPingSourceCloudEventAttributes,
rtv1.WithPingSourceDeleted,
rtv1.WithPingSourceOIDCIdentityCreatedSucceededBecauseOIDCFeatureDisabled(),
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved
),
},
WantErr: false,
Expand Down
22 changes: 21 additions & 1 deletion pkg/apis/sources/v1/ping_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ const (

// PingSourceConditionDeployed has status True when the PingSource has had it's receive adapter deployment created.
PingSourceConditionDeployed apis.ConditionType = "Deployed"

// PingSourceConditionOIDCIdentityCreated has status True when the PingSource has had it's OIDC identity created.
PingSourceConditionOIDCIdentityCreated apis.ConditionType = "OIDCIdentityCreated"
)

var PingSourceCondSet = apis.NewLivingConditionSet(
PingSourceConditionSinkProvided,
PingSourceConditionDeployed)
PingSourceConditionDeployed,
PingSourceConditionOIDCIdentityCreated)

const (
// PingSourceEventType is the default PingSource CloudEvent type.
Expand Down Expand Up @@ -122,3 +126,19 @@ func (s *PingSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deployment)
PingSourceCondSet.Manage(s).MarkUnknown(PingSourceConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}

func (s *PingSourceStatus) MarkOIDCIdentityCreatedSucceeded() {
PingSourceCondSet.Manage(s).MarkTrue(PingSourceConditionOIDCIdentityCreated)
}

func (s *PingSourceStatus) MarkOIDCIdentityCreatedSucceededWithReason(reason, messageFormat string, messageA ...interface{}) {
PingSourceCondSet.Manage(s).MarkTrueWithReason(PingSourceConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}

func (s *PingSourceStatus) MarkOIDCIdentityCreatedFailed(reason, messageFormat string, messageA ...interface{}) {
PingSourceCondSet.Manage(s).MarkFalse(PingSourceConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}

func (s *PingSourceStatus) MarkOIDCIdentityCreatedUnknown(reason, messageFormat string, messageA ...interface{}) {
PingSourceCondSet.Manage(s).MarkUnknown(PingSourceConditionOIDCIdentityCreated, reason, messageFormat, messageA...)
}
139 changes: 113 additions & 26 deletions pkg/apis/sources/v1/ping_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ func TestPingSourceStatusIsReady(t *testing.T) {
}

tests := []struct {
name string
s *PingSourceStatus
wantConditionStatus corev1.ConditionStatus
want bool
name string
s *PingSourceStatus
wantConditionStatus corev1.ConditionStatus
want bool
oidcServiceAccountStatus bool
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved
}{{
name: "uninitialized",
s: &PingSourceStatus{},
want: false,
name: "uninitialized",
s: &PingSourceStatus{},
want: false,
oidcServiceAccountStatus: true,
}, {
name: "initialized",
s: func() *PingSourceStatus {
s := &PingSourceStatus{}
s.InitializeConditions()
return s
}(),
wantConditionStatus: corev1.ConditionUnknown,
want: false,
wantConditionStatus: corev1.ConditionUnknown,
want: false,
oidcServiceAccountStatus: true,
}, {
name: "mark deployed",
s: func() *PingSourceStatus {
Expand All @@ -84,8 +87,9 @@ func TestPingSourceStatusIsReady(t *testing.T) {
s.PropagateDeploymentAvailability(availableDeployment)
return s
}(),
wantConditionStatus: corev1.ConditionUnknown,
want: false,
wantConditionStatus: corev1.ConditionUnknown,
want: false,
oidcServiceAccountStatus: true,
}, {
name: "mark sink",
s: func() *PingSourceStatus {
Expand All @@ -95,8 +99,9 @@ func TestPingSourceStatusIsReady(t *testing.T) {
s.MarkSink(exampleAddr)
return s
}(),
wantConditionStatus: corev1.ConditionUnknown,
want: false,
wantConditionStatus: corev1.ConditionUnknown,
want: false,
oidcServiceAccountStatus: true,
}, {
name: "mark sink and deployed",
s: func() *PingSourceStatus {
Expand All @@ -106,12 +111,33 @@ func TestPingSourceStatusIsReady(t *testing.T) {
s.PropagateDeploymentAvailability(availableDeployment)
return s
}(),
wantConditionStatus: corev1.ConditionTrue,
want: true,
}}
wantConditionStatus: corev1.ConditionTrue,
want: true,
oidcServiceAccountStatus: true,
},
{
name: "oidc status false",
s: func() *PingSourceStatus {
s := &PingSourceStatus{}
s.InitializeConditions()
s.MarkSink(exampleAddr)
s.PropagateDeploymentAvailability(availableDeployment)
return s
}(),
wantConditionStatus: corev1.ConditionFalse,
want: false,
oidcServiceAccountStatus: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.oidcServiceAccountStatus {
test.s.MarkOIDCIdentityCreatedSucceeded()
} else {
test.s.MarkOIDCIdentityCreatedFailed("Unable to ...", "")
}

if test.wantConditionStatus != "" {
gotConditionStatus := test.s.GetTopLevelCondition().Status
if gotConditionStatus != test.wantConditionStatus {
Expand All @@ -122,6 +148,7 @@ func TestPingSourceStatusIsReady(t *testing.T) {
if got != test.want {
t.Errorf("unexpected readiness: want %v, got %v", test.want, got)
}

})
}
}
Expand All @@ -133,13 +160,18 @@ func TestPingSourceStatusGetTopLevelCondition(t *testing.T) {
}

tests := []struct {
name string
s *PingSourceStatus
want *apis.Condition
name string
s *PingSourceStatus
want *apis.Condition
oidcServiceAccountStatus bool
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved
}{{
name: "uninitialized",
s: &PingSourceStatus{},
want: nil,
want: &apis.Condition{
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "initialized",
s: func() *PingSourceStatus {
Expand All @@ -151,6 +183,7 @@ func TestPingSourceStatusGetTopLevelCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "mark deployed",
s: func() *PingSourceStatus {
Expand All @@ -163,6 +196,7 @@ func TestPingSourceStatusGetTopLevelCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "mark sink",
s: func() *PingSourceStatus {
Expand All @@ -175,6 +209,7 @@ func TestPingSourceStatusGetTopLevelCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "mark sink and deployed",
s: func() *PingSourceStatus {
Expand All @@ -188,10 +223,34 @@ func TestPingSourceStatusGetTopLevelCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionTrue,
},
}}
oidcServiceAccountStatus: true,
},
{
name: "oidc fail",
s: func() *PingSourceStatus {
s := &PingSourceStatus{}
s.InitializeConditions()
s.MarkSink(exampleAddr)
s.PropagateDeploymentAvailability(availableDeployment)
return s
}(),
want: &apis.Condition{
Type: PingSourceConditionReady,
Status: corev1.ConditionFalse,
Reason: "Unable to ...",
},
oidcServiceAccountStatus: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.oidcServiceAccountStatus {
test.s.MarkOIDCIdentityCreatedSucceeded()
} else {
test.s.MarkOIDCIdentityCreatedFailed("Unable to ...", "")
}

got := test.s.GetTopLevelCondition()
ignoreTime := cmpopts.IgnoreFields(apis.Condition{},
"LastTransitionTime", "Severity")
Expand All @@ -209,15 +268,20 @@ func TestPingSourceStatusGetCondition(t *testing.T) {
}

tests := []struct {
name string
s *PingSourceStatus
condQuery apis.ConditionType
want *apis.Condition
name string
s *PingSourceStatus
condQuery apis.ConditionType
want *apis.Condition
oidcServiceAccountStatus bool
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved
}{{
name: "uninitialized",
s: &PingSourceStatus{},
condQuery: PingSourceConditionReady,
want: nil,
want: &apis.Condition{
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "initialized",
s: func() *PingSourceStatus {
Expand All @@ -230,6 +294,7 @@ func TestPingSourceStatusGetCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "mark deployed",
s: func() *PingSourceStatus {
Expand All @@ -243,6 +308,7 @@ func TestPingSourceStatusGetCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "mark sink",
s: func() *PingSourceStatus {
Expand All @@ -256,10 +322,31 @@ func TestPingSourceStatusGetCondition(t *testing.T) {
Type: PingSourceConditionReady,
Status: corev1.ConditionUnknown,
},
oidcServiceAccountStatus: true,
}, {
name: "oidc failed",
s: func() *PingSourceStatus {
s := &PingSourceStatus{}
s.InitializeConditions()
s.MarkSink(exampleAddr)
return s
}(),
condQuery: PingSourceConditionReady,
want: &apis.Condition{
Type: PingSourceConditionReady,
Status: corev1.ConditionFalse,
Reason: "Unable to ...",
},
oidcServiceAccountStatus: false,
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.oidcServiceAccountStatus {
test.s.MarkOIDCIdentityCreatedSucceeded()
} else {
test.s.MarkOIDCIdentityCreatedFailed("Unable to ...", "")
}
got := test.s.GetCondition(test.condQuery)
ignoreTime := cmpopts.IgnoreFields(apis.Condition{},
"LastTransitionTime", "Severity")
Expand Down
Loading