Skip to content

Commit

Permalink
feat: add resource MonitoringServiceLevelObjective
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Feb 8, 2025
1 parent ad50980 commit 52a79a7
Show file tree
Hide file tree
Showing 8 changed files with 1,201 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apis/monitoring/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +kcc:proto=google.monitoring.v3
package v1alpha1
33 changes: 33 additions & 0 deletions apis/monitoring/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +kubebuilder:object:generate=true
// +groupName=monitoring.cnrm.cloud.google.com
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "monitoring.cnrm.cloud.google.com", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
118 changes: 118 additions & 0 deletions apis/monitoring/v1alpha1/servicelevelobjective_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common"
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ServiceLevelObjectiveIdentity defines the resource reference to MonitoringServiceLevelObjective, which "External" field
// holds the GCP identifier for the KRM object.
type ServiceLevelObjectiveIdentity struct {
parent *ServiceLevelObjectiveParent
id string
}

func (i *ServiceLevelObjectiveIdentity) String() string {
return i.parent.String() + "/servicelevelobjectives/" + i.id
}

func (i *ServiceLevelObjectiveIdentity) ID() string {
return i.id
}

func (i *ServiceLevelObjectiveIdentity) Parent() *ServiceLevelObjectiveParent {
return i.parent
}

type ServiceLevelObjectiveParent struct {
ProjectID string
Location string
}

func (p *ServiceLevelObjectiveParent) String() string {
return "projects/" + p.ProjectID + "/locations/" + p.Location
}


// New builds a ServiceLevelObjectiveIdentity from the Config Connector ServiceLevelObjective object.
func NewServiceLevelObjectiveIdentity(ctx context.Context, reader client.Reader, obj *MonitoringServiceLevelObjective) (*ServiceLevelObjectiveIdentity, error) {

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), obj.Spec.ProjectRef)

Check failure on line 60 in apis/monitoring/v1alpha1/servicelevelobjective_identity.go

View workflow job for this annotation

GitHub Actions / lint

obj.Spec.ProjectRef undefined (type MonitoringServiceLevelObjectiveSpec has no field or method ProjectRef)

Check failure on line 60 in apis/monitoring/v1alpha1/servicelevelobjective_identity.go

View workflow job for this annotation

GitHub Actions / lint

obj.Spec.ProjectRef undefined (type MonitoringServiceLevelObjectiveSpec has no field or method ProjectRef)
if err != nil {
return nil, err
}
projectID := projectRef.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
location := obj.Spec.Location

Check failure on line 68 in apis/monitoring/v1alpha1/servicelevelobjective_identity.go

View workflow job for this annotation

GitHub Actions / lint

obj.Spec.Location undefined (type MonitoringServiceLevelObjectiveSpec has no field or method Location)

Check failure on line 68 in apis/monitoring/v1alpha1/servicelevelobjective_identity.go

View workflow job for this annotation

GitHub Actions / lint

obj.Spec.Location undefined (type MonitoringServiceLevelObjectiveSpec has no field or method Location)

// Get desired ID
resourceID := common.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}

// Use approved External
externalRef := common.ValueOf(obj.Status.ExternalRef)
if externalRef != "" {
// Validate desired with actual
actualParent, actualResourceID, err := ParseServiceLevelObjectiveExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID)
}
if actualParent.Location != location {
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, location)
}
if actualResourceID != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualResourceID)
}
}
return &ServiceLevelObjectiveIdentity{
parent: &ServiceLevelObjectiveParent{
ProjectID: projectID,
Location: location,
},
id: resourceID,
}, nil
}

func ParseServiceLevelObjectiveExternal(external string) (parent *ServiceLevelObjectiveParent, resourceID string, err error) {
tokens := strings.Split(external, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "servicelevelobjectives" {
return nil, "", fmt.Errorf("format of MonitoringServiceLevelObjective external=%q was not known (use projects/{{projectID}}/locations/{{location}}/servicelevelobjectives/{{servicelevelobjectiveID}})", external)
}
parent = &ServiceLevelObjectiveParent{
ProjectID: tokens[1],
Location: tokens[3],
}
resourceID = tokens[5]
return parent, resourceID, nil
}
83 changes: 83 additions & 0 deletions apis/monitoring/v1alpha1/servicelevelobjective_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"context"
"fmt"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ refsv1beta1.ExternalNormalizer = &ServiceLevelObjectiveRef{}

// ServiceLevelObjectiveRef defines the resource reference to MonitoringServiceLevelObjective, which "External" field
// holds the GCP identifier for the KRM object.
type ServiceLevelObjectiveRef struct {
// A reference to an externally managed MonitoringServiceLevelObjective resource.
// Should be in the format "projects/{{projectID}}/locations/{{location}}/servicelevelobjectives/{{servicelevelobjectiveID}}".
External string `json:"external,omitempty"`

// The name of a MonitoringServiceLevelObjective resource.
Name string `json:"name,omitempty"`

// The namespace of a MonitoringServiceLevelObjective resource.
Namespace string `json:"namespace,omitempty"`
}

// NormalizedExternal provision the "External" value for other resource that depends on MonitoringServiceLevelObjective.
// If the "External" is given in the other resource's spec.MonitoringServiceLevelObjectiveRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual MonitoringServiceLevelObjective object from the cluster.
func (r *ServiceLevelObjectiveRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) {
if r.External != "" && r.Name != "" {
return "", fmt.Errorf("cannot specify both name and external on %s reference", MonitoringServiceLevelObjectiveGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := ParseServiceLevelObjectiveExternal(r.External); err != nil {
return "", err
}
return r.External, nil
}

// From the Config Connector object
if r.Namespace == "" {
r.Namespace = otherNamespace
}
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(MonitoringServiceLevelObjectiveGVK)
if err := reader.Get(ctx, key, u); err != nil {
if apierrors.IsNotFound(err) {
return "", k8s.NewReferenceNotFoundError(u.GroupVersionKind(), key)
}
return "", fmt.Errorf("reading referenced %s %s: %w", MonitoringServiceLevelObjectiveGVK, key, err)
}
// Get external from status.externalRef. This is the most trustworthy place.
actualExternalRef, _, err := unstructured.NestedString(u.Object, "status", "externalRef")
if err != nil {
return "", fmt.Errorf("reading status.externalRef: %w", err)
}
if actualExternalRef == "" {
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key)
}
r.External = actualExternalRef
return r.External, nil
}
84 changes: 84 additions & 0 deletions apis/monitoring/v1alpha1/servicelevelobjective_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var MonitoringServiceLevelObjectiveGVK = GroupVersion.WithKind("MonitoringServiceLevelObjective")

// MonitoringServiceLevelObjectiveSpec defines the desired state of MonitoringServiceLevelObjective
// +kcc:proto=google.monitoring.v3.ServiceLevelObjective
type MonitoringServiceLevelObjectiveSpec struct {
// The MonitoringServiceLevelObjective name. If not given, the metadata.name will be used.
ResourceID *string `json:"resourceID,omitempty"`
}

// MonitoringServiceLevelObjectiveStatus defines the config connector machine state of MonitoringServiceLevelObjective
type MonitoringServiceLevelObjectiveStatus struct {
/* Conditions represent the latest available observations of the
object's current state. */
Conditions []v1alpha1.Condition `json:"conditions,omitempty"`

// ObservedGeneration is the generation of the resource that was most recently observed by the Config Connector controller. If this is equal to metadata.generation, then that means that the current reported status reflects the most recent desired state of the resource.
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`

// A unique specifier for the MonitoringServiceLevelObjective resource in GCP.
ExternalRef *string `json:"externalRef,omitempty"`

// ObservedState is the state of the resource as most recently observed in GCP.
ObservedState *MonitoringServiceLevelObjectiveObservedState `json:"observedState,omitempty"`
}

// MonitoringServiceLevelObjectiveObservedState is the state of the MonitoringServiceLevelObjective resource as most recently observed in GCP.
// +kcc:proto=google.monitoring.v3.ServiceLevelObjective
type MonitoringServiceLevelObjectiveObservedState struct {
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// TODO(user): make sure the pluralizaiton below is correct
// +kubebuilder:resource:categories=gcp,shortName=gcpmonitoringservicelevelobjective;gcpmonitoringservicelevelobjectives
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/system=true"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
// +kubebuilder:printcolumn:name="Ready",JSONPath=".status.conditions[?(@.type=='Ready')].status",type="string",description="When 'True', the most recent reconcile of the resource succeeded"
// +kubebuilder:printcolumn:name="Status",JSONPath=".status.conditions[?(@.type=='Ready')].reason",type="string",description="The reason for the value in 'Ready'"
// +kubebuilder:printcolumn:name="Status Age",JSONPath=".status.conditions[?(@.type=='Ready')].lastTransitionTime",type="date",description="The last transition time for the value in 'Status'"

// MonitoringServiceLevelObjective is the Schema for the MonitoringServiceLevelObjective API
// +k8s:openapi-gen=true
type MonitoringServiceLevelObjective struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// +required
Spec MonitoringServiceLevelObjectiveSpec `json:"spec,omitempty"`
Status MonitoringServiceLevelObjectiveStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// MonitoringServiceLevelObjectiveList contains a list of MonitoringServiceLevelObjective
type MonitoringServiceLevelObjectiveList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MonitoringServiceLevelObjective `json:"items"`
}

func init() {
SchemeBuilder.Register(&MonitoringServiceLevelObjective{}, &MonitoringServiceLevelObjectiveList{})

Check failure on line 83 in apis/monitoring/v1alpha1/servicelevelobjective_types.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &MonitoringServiceLevelObjective{} (value of type *MonitoringServiceLevelObjective) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *MonitoringServiceLevelObjective does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)

Check failure on line 83 in apis/monitoring/v1alpha1/servicelevelobjective_types.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &MonitoringServiceLevelObjectiveList{} (value of type *MonitoringServiceLevelObjectiveList) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *MonitoringServiceLevelObjectiveList does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject) (typecheck)

Check failure on line 83 in apis/monitoring/v1alpha1/servicelevelobjective_types.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &MonitoringServiceLevelObjective{} (value of type *MonitoringServiceLevelObjective) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *MonitoringServiceLevelObjective does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)

Check failure on line 83 in apis/monitoring/v1alpha1/servicelevelobjective_types.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &MonitoringServiceLevelObjectiveList{} (value of type *MonitoringServiceLevelObjectiveList) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *MonitoringServiceLevelObjectiveList does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)) (typecheck)
}
Loading

0 comments on commit 52a79a7

Please sign in to comment.