Skip to content

Commit

Permalink
feat: support setting loggingService and monitoringService
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweaver87 committed Jul 29, 2024
1 parent 890c3cb commit 3e1996f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
}
if !s.scope.IsAutopilotCluster() {
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.GetName())
if s.scope.GCPManagedControlPlane.Spec.LoggingService != nil {
cluster.LoggingService = s.scope.GCPManagedControlPlane.Spec.LoggingService.String()
}
if s.scope.GCPManagedControlPlane.Spec.MonitoringService != nil {
cluster.MonitoringService = s.scope.GCPManagedControlPlane.Spec.MonitoringService.String()
}
}

createClusterRequest := &containerpb.CreateClusterRequest{
Expand Down Expand Up @@ -431,6 +437,20 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster
}
}

// LoggingService
if existingCluster.GetLoggingService() != s.scope.GCPManagedControlPlane.Spec.LoggingService.String() {
needUpdate = true
clusterUpdate.DesiredLoggingService = s.scope.GCPManagedControlPlane.Spec.LoggingService.String()
log.V(2).Info("LoggingService config update required", "current", existingCluster.GetLoggingService(), "desired", s.scope.GCPManagedControlPlane.Spec.LoggingService.String())
}

// MonitoringService
if existingCluster.GetMonitoringService() != s.scope.GCPManagedControlPlane.Spec.MonitoringService.String() {
needUpdate = true
clusterUpdate.DesiredLoggingService = s.scope.GCPManagedControlPlane.Spec.MonitoringService.String()
log.V(2).Info("MonitoringService config update required", "current", existingCluster.GetMonitoringService(), "desired", s.scope.GCPManagedControlPlane.Spec.MonitoringService.String())
}

// DesiredMasterAuthorizedNetworksConfig
// When desiredMasterAuthorizedNetworksConfig is nil, it means that the user wants to disable the feature.
desiredMasterAuthorizedNetworksConfig := convertToSdkMasterAuthorizedNetworksConfig(s.scope.GCPManagedControlPlane.Spec.MasterAuthorizedNetworksConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ spec:
Location represents the location (region or zone) in which the GKE cluster
will be created.
type: string
loggingService:
description: |-
LoggingService represents configuration of logging service feature of the GKE cluster.
Possible values: none, logging.googleapis.com/kubernetes (default).
Value is ignored when enableAutopilot = true.
type: string
master_authorized_networks_config:
description: |-
MasterAuthorizedNetworksConfig represents configuration options for master authorized networks feature of the GKE cluster.
Expand Down Expand Up @@ -189,6 +195,12 @@ spec:
Public IP addresses.
type: boolean
type: object
monitoringService:
description: |-
MonitoringService represents configuration of monitoring service feature of the GKE cluster.
Possible values: none, monitoring.googleapis.com/kubernetes (default).
Value is ignored when enableAutopilot = true.
type: string
project:
description: Project is the name of the project to deploy the cluster
to.
Expand Down
50 changes: 50 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
package v1beta1

import (
"fmt"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/strings/slices"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

Expand Down Expand Up @@ -148,6 +152,16 @@ type GCPManagedControlPlaneSpec struct {
// This feature is disabled if this field is not specified.
// +optional
MasterAuthorizedNetworksConfig *MasterAuthorizedNetworksConfig `json:"master_authorized_networks_config,omitempty"`
// LoggingService represents configuration of logging service feature of the GKE cluster.
// Possible values: none, logging.googleapis.com/kubernetes (default).
// Value is ignored when enableAutopilot = true.
// +optional
LoggingService *LoggingService `json:"loggingService,omitempty"`
// MonitoringService represents configuration of monitoring service feature of the GKE cluster.
// Possible values: none, monitoring.googleapis.com/kubernetes (default).
// Value is ignored when enableAutopilot = true.
// +optional
MonitoringService *MonitoringService `json:"monitoringService,omitempty"`
}

// GCPManagedControlPlaneStatus defines the observed state of GCPManagedControlPlane.
Expand Down Expand Up @@ -233,6 +247,42 @@ type MasterAuthorizedNetworksConfigCidrBlock struct {
CidrBlock string `json:"cidr_block,omitempty"`
}

// LoggingService is GKE logging service configuration.
type LoggingService string

// Validate validates LoggingService value.
func (l LoggingService) Validate() error {
validValues := []string{"none", "logging.googleapis.com/kubernetes"}
if !slices.Contains(validValues, l.String()) {
return fmt.Errorf("invalid value; expect one of : %s", strings.Join(validValues, ","))
}

return nil
}

// String returns a string from LoggingService.
func (l LoggingService) String() string {
return string(l)
}

// MonitoringService is GKE logging service configuration.
type MonitoringService string

// Validate validates MonitoringService value.
func (m MonitoringService) Validate() error {
validValues := []string{"none", "monitoring.googleapis.com/kubernetes"}
if !slices.Contains(validValues, m.String()) {
return fmt.Errorf("invalid value; expect one of : %s", strings.Join(validValues, ","))
}

return nil
}

// String returns a string from MonitoringService.
func (m MonitoringService) String() string {
return string(m)
}

// GetConditions returns the control planes conditions.
func (r *GCPManagedControlPlane) GetConditions() clusterv1.Conditions {
return r.Status.Conditions
Expand Down
37 changes: 36 additions & 1 deletion exp/api/v1beta1/gcpmanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"github.com/google/go-cmp/cmp"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/validation/field"

Expand Down Expand Up @@ -89,6 +88,16 @@ func (r *GCPManagedControlPlane) ValidateCreate() (admission.Warnings, error) {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "ReleaseChannel"), "Release channel is required for an autopilot enabled cluster"))
}

if r.Spec.EnableAutopilot && r.Spec.LoggingService != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "LoggingService"),
r.Spec.LoggingService, "can't be set when autopilot is enabled"))
}

if r.Spec.EnableAutopilot && r.Spec.MonitoringService != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "MonitoringService"),
r.Spec.LoggingService, "can't be set when autopilot is enabled"))
}

if len(allErrs) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -130,6 +139,32 @@ func (r *GCPManagedControlPlane) ValidateUpdate(oldRaw runtime.Object) (admissio
)
}

if old.Spec.EnableAutopilot && r.Spec.LoggingService != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "LoggingService"),
r.Spec.LoggingService, "can't be set when autopilot is enabled"))
}

if old.Spec.EnableAutopilot && r.Spec.MonitoringService != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "MonitoringService"),
r.Spec.LoggingService, "can't be set when autopilot is enabled"))
}

if r.Spec.LoggingService != nil {
err := r.Spec.LoggingService.Validate()
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "LoggingService"),
r.Spec.LoggingService, err.Error()))
}
}

if r.Spec.MonitoringService != nil {
err := r.Spec.MonitoringService.Validate()
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "MonitoringService"),
r.Spec.MonitoringService, err.Error()))
}
}

if len(allErrs) == 0 {
return nil, nil
}
Expand Down
10 changes: 10 additions & 0 deletions exp/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e1996f

Please sign in to comment.