Skip to content

Commit b2d8132

Browse files
committed
Fixed webhook and added webhook tests.
1 parent 988d218 commit b2d8132

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

api/v1beta2/ocimanagedcontrolplane_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20-
"github.com/oracle/oci-go-sdk/v65/common"
2120
apierrors "k8s.io/apimachinery/pkg/api/errors"
2221
"k8s.io/apimachinery/pkg/runtime"
2322
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -58,7 +57,7 @@ func (c *OCIManagedControlPlane) ValidateCreate() (admission.Warnings, error) {
5857
allErrs = append(allErrs, field.Invalid(field.NewPath("Name"), c.Name, "Name cannot be more than 31 characters"))
5958
}
6059

61-
if c.Spec.ClusterOption.OpenIdConnectTokenAuthenticationConfig.IsOpenIdConnectAuthEnabled == *common.Bool(true) {
60+
if c.Spec.ClusterOption.OpenIdConnectTokenAuthenticationConfig != nil && c.Spec.ClusterOption.OpenIdConnectTokenAuthenticationConfig.IsOpenIdConnectAuthEnabled {
6261
if c.Spec.ClusterType != EnhancedClusterType {
6362
allErrs = append(allErrs, field.Invalid(field.NewPath("ClusterType"), c.Spec.ClusterType, "ClusterType needs to be set to ENHANCED_CLUSTER for OpenIdConnectTokenAuthenticationConfig to be enabled."))
6463
}

api/v1beta2/ocimanagedcontrolplane_webhook_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/oracle/oci-go-sdk/v65/common"
24+
2325
"github.com/onsi/gomega"
2426
. "github.com/onsi/gomega"
2527
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -79,6 +81,85 @@ func TestOCIManagedControlPlane_ValidateCreate(t *testing.T) {
7981
},
8082
expectErr: false,
8183
},
84+
{
85+
name: "OpenIdConnectAuthEnabledWithValidConfig",
86+
c: &OCIManagedControlPlane{
87+
Spec: OCIManagedControlPlaneSpec{
88+
ClusterType: EnhancedClusterType,
89+
ClusterOption: ClusterOptions{
90+
OpenIdConnectTokenAuthenticationConfig: &OpenIDConnectTokenAuthenticationConfig{
91+
IsOpenIdConnectAuthEnabled: *common.Bool(true),
92+
ClientId: common.String("client-id"),
93+
IssuerUrl: common.String("issuer-url"),
94+
},
95+
},
96+
},
97+
},
98+
expectErr: false,
99+
},
100+
{
101+
name: "OpenIdConnectAuthEnabledWithInvalidClusterType",
102+
c: &OCIManagedControlPlane{
103+
Spec: OCIManagedControlPlaneSpec{
104+
ClusterType: BasicClusterType,
105+
ClusterOption: ClusterOptions{
106+
OpenIdConnectTokenAuthenticationConfig: &OpenIDConnectTokenAuthenticationConfig{
107+
IsOpenIdConnectAuthEnabled: *common.Bool(true),
108+
ClientId: common.String("client-id"),
109+
IssuerUrl: common.String("issuer-url"),
110+
},
111+
},
112+
},
113+
},
114+
errorMgsShouldContain: "ClusterType needs to be set to ENHANCED_CLUSTER for OpenIdConnectTokenAuthenticationConfig to be enabled.",
115+
expectErr: true,
116+
},
117+
{
118+
name: "OpenIdConnectAuthEnabledWithMissingClientId",
119+
c: &OCIManagedControlPlane{
120+
Spec: OCIManagedControlPlaneSpec{
121+
ClusterType: EnhancedClusterType,
122+
ClusterOption: ClusterOptions{
123+
OpenIdConnectTokenAuthenticationConfig: &OpenIDConnectTokenAuthenticationConfig{
124+
IsOpenIdConnectAuthEnabled: *common.Bool(true),
125+
IssuerUrl: common.String("issuer-url"),
126+
},
127+
},
128+
},
129+
},
130+
errorMgsShouldContain: "ClientId cannot be empty when OpenIdConnectAuth is enabled.",
131+
expectErr: true,
132+
},
133+
{
134+
name: "OpenIdConnectAuthEnabledWithMissingIssuerUrl",
135+
c: &OCIManagedControlPlane{
136+
Spec: OCIManagedControlPlaneSpec{
137+
ClusterType: EnhancedClusterType,
138+
ClusterOption: ClusterOptions{
139+
OpenIdConnectTokenAuthenticationConfig: &OpenIDConnectTokenAuthenticationConfig{
140+
IsOpenIdConnectAuthEnabled: *common.Bool(true),
141+
ClientId: common.String("client-id"),
142+
},
143+
},
144+
},
145+
},
146+
errorMgsShouldContain: "IssuerUrl cannot be empty when OpenIdConnectAuth is enabled.",
147+
expectErr: true,
148+
},
149+
{
150+
name: "OpenIdConnectAuthDisabled",
151+
c: &OCIManagedControlPlane{
152+
Spec: OCIManagedControlPlaneSpec{
153+
ClusterType: BasicClusterType,
154+
ClusterOption: ClusterOptions{
155+
OpenIdConnectTokenAuthenticationConfig: &OpenIDConnectTokenAuthenticationConfig{
156+
IsOpenIdConnectAuthEnabled: *common.Bool(false),
157+
},
158+
},
159+
},
160+
},
161+
expectErr: false,
162+
},
82163
}
83164
for _, test := range tests {
84165
t.Run(test.name, func(t *testing.T) {

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ spec:
88
spec:
99
containers:
1010
# Change the value of image field below to your controller image URL
11-
- image: ghcr.io/oracle/cluster-api-oci-controller-amd64:dev
11+
- image: ghcr.io/oracle/cluster-api-oci-controller-amd64:dev
1212
name: manager

0 commit comments

Comments
 (0)