Skip to content

Commit fc2e057

Browse files
committed
fix: validate values
1 parent a3bce87 commit fc2e057

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

cloud/services/container/clusters/reconcile.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
297297
if !s.scope.IsAutopilotCluster() {
298298
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.GetName())
299299
if s.scope.GCPManagedControlPlane.Spec.LoggingService != nil {
300-
cluster.LoggingService = *s.scope.GCPManagedControlPlane.Spec.LoggingService
300+
cluster.LoggingService = s.scope.GCPManagedControlPlane.Spec.LoggingService.String()
301301
}
302302
if s.scope.GCPManagedControlPlane.Spec.MonitoringService != nil {
303-
cluster.MonitoringService = *s.scope.GCPManagedControlPlane.Spec.MonitoringService
303+
cluster.MonitoringService = s.scope.GCPManagedControlPlane.Spec.MonitoringService.String()
304304
}
305305
}
306306

@@ -437,17 +437,17 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster
437437
}
438438

439439
// LoggingService
440-
if existingCluster.GetLoggingService() != *s.scope.GCPManagedControlPlane.Spec.LoggingService {
440+
if existingCluster.GetLoggingService() != s.scope.GCPManagedControlPlane.Spec.LoggingService.String() {
441441
needUpdate = true
442-
clusterUpdate.DesiredLoggingService = *s.scope.GCPManagedControlPlane.Spec.LoggingService
443-
log.V(2).Info("LoggingService config update required", "current", existingCluster.GetLoggingService(), "desired", *s.scope.GCPManagedControlPlane.Spec.LoggingService)
442+
clusterUpdate.DesiredLoggingService = s.scope.GCPManagedControlPlane.Spec.LoggingService.String()
443+
log.V(2).Info("LoggingService config update required", "current", existingCluster.GetLoggingService(), "desired", s.scope.GCPManagedControlPlane.Spec.LoggingService.String())
444444
}
445445

446446
// MonitoringService
447-
if existingCluster.GetMonitoringService() != *s.scope.GCPManagedControlPlane.Spec.MonitoringService {
447+
if existingCluster.GetMonitoringService() != s.scope.GCPManagedControlPlane.Spec.MonitoringService.String() {
448448
needUpdate = true
449-
clusterUpdate.DesiredLoggingService = *s.scope.GCPManagedControlPlane.Spec.MonitoringService
450-
log.V(2).Info("MonitoringService config update required", "current", existingCluster.GetMonitoringService(), "desired", *s.scope.GCPManagedControlPlane.Spec.MonitoringService)
449+
clusterUpdate.DesiredLoggingService = s.scope.GCPManagedControlPlane.Spec.MonitoringService.String()
450+
log.V(2).Info("MonitoringService config update required", "current", existingCluster.GetMonitoringService(), "desired", s.scope.GCPManagedControlPlane.Spec.MonitoringService.String())
451451
}
452452

453453
// DesiredMasterAuthorizedNetworksConfig

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmanagedcontrolplanes.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ spec:
164164
loggingService:
165165
description: |-
166166
LoggingService represents configuration of logging service feature of the GKE cluster.
167-
Possible values: none, logging.googleapis.com/kubernetes (default for GKE 1.14+), logging.googleapis.com (default for GKE < 1.14).
167+
Possible values: none, logging.googleapis.com/kubernetes (default).
168168
Value is ignored when enableAutopilot = true.
169169
type: string
170170
master_authorized_networks_config:
@@ -198,7 +198,7 @@ spec:
198198
monitoringService:
199199
description: |-
200200
MonitoringService represents configuration of monitoring service feature of the GKE cluster.
201-
Possible values: none, monitoring.googleapis.com/kubernetes (default for GKE 1.14+), monitoring.googleapis.com (default for GKE < 1.14).
201+
Possible values: none, monitoring.googleapis.com/kubernetes (default).
202202
Value is ignored when enableAutopilot = true.
203203
type: string
204204
project:

exp/api/v1beta1/gcpmanagedcontrolplane_types.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/utils/strings/slices"
2125
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2226
)
2327

@@ -149,15 +153,15 @@ type GCPManagedControlPlaneSpec struct {
149153
// +optional
150154
MasterAuthorizedNetworksConfig *MasterAuthorizedNetworksConfig `json:"master_authorized_networks_config,omitempty"`
151155
// LoggingService represents configuration of logging service feature of the GKE cluster.
152-
// Possible values: none, logging.googleapis.com/kubernetes (default for GKE 1.14+), logging.googleapis.com (default for GKE < 1.14).
156+
// Possible values: none, logging.googleapis.com/kubernetes (default).
153157
// Value is ignored when enableAutopilot = true.
154158
// +optional
155-
LoggingService *string `json:"loggingService,omitempty"`
159+
LoggingService *LoggingService `json:"loggingService,omitempty"`
156160
// MonitoringService represents configuration of monitoring service feature of the GKE cluster.
157-
// Possible values: none, monitoring.googleapis.com/kubernetes (default for GKE 1.14+), monitoring.googleapis.com (default for GKE < 1.14).
161+
// Possible values: none, monitoring.googleapis.com/kubernetes (default).
158162
// Value is ignored when enableAutopilot = true.
159163
// +optional
160-
MonitoringService *string `json:"monitoringService,omitempty"`
164+
MonitoringService *MonitoringService `json:"monitoringService,omitempty"`
161165
}
162166

163167
// GCPManagedControlPlaneStatus defines the observed state of GCPManagedControlPlane.
@@ -243,6 +247,42 @@ type MasterAuthorizedNetworksConfigCidrBlock struct {
243247
CidrBlock string `json:"cidr_block,omitempty"`
244248
}
245249

250+
// LoggingService is GKE logging service configuration.
251+
type LoggingService string
252+
253+
// Validate validates LoggingService value.
254+
func (l LoggingService) Validate() error {
255+
validValues := []string{"none", "logging.googleapis.com/kubernetes"}
256+
if !slices.Contains(validValues, l.String()) {
257+
return fmt.Errorf("invalid value; expect one of : %s", strings.Join(validValues, ","))
258+
}
259+
260+
return nil
261+
}
262+
263+
// String returns a string from LoggingService.
264+
func (l LoggingService) String() string {
265+
return string(l)
266+
}
267+
268+
// MonitoringService is GKE logging service configuration.
269+
type MonitoringService string
270+
271+
// Validate validates MonitoringService value.
272+
func (m MonitoringService) Validate() error {
273+
validValues := []string{"none", "monitoring.googleapis.com/kubernetes"}
274+
if !slices.Contains(validValues, m.String()) {
275+
return fmt.Errorf("invalid value; expect one of : %s", strings.Join(validValues, ","))
276+
}
277+
278+
return nil
279+
}
280+
281+
// String returns a string from MonitoringService.
282+
func (m MonitoringService) String() string {
283+
return string(m)
284+
}
285+
246286
// GetConditions returns the control planes conditions.
247287
func (r *GCPManagedControlPlane) GetConditions() clusterv1.Conditions {
248288
return r.Status.Conditions

exp/api/v1beta1/gcpmanagedcontrolplane_webhook.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
"github.com/google/go-cmp/cmp"
24-
2524
apierrors "k8s.io/apimachinery/pkg/api/errors"
2625
"k8s.io/apimachinery/pkg/util/validation/field"
2726

@@ -150,6 +149,22 @@ func (r *GCPManagedControlPlane) ValidateUpdate(oldRaw runtime.Object) (admissio
150149
r.Spec.LoggingService, "can't be set when autopilot is enabled"))
151150
}
152151

152+
if r.Spec.LoggingService != nil {
153+
err := r.Spec.LoggingService.Validate()
154+
if err != nil {
155+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "LoggingService"),
156+
r.Spec.LoggingService, err.Error()))
157+
}
158+
}
159+
160+
if r.Spec.MonitoringService != nil {
161+
err := r.Spec.MonitoringService.Validate()
162+
if err != nil {
163+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "MonitoringService"),
164+
r.Spec.MonitoringService, err.Error()))
165+
}
166+
}
167+
153168
if len(allErrs) == 0 {
154169
return nil, nil
155170
}

exp/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)