Skip to content

Commit

Permalink
Merge branch 'GoogleCloudPlatform:master' into handle_identity
Browse files Browse the repository at this point in the history
  • Loading branch information
nb-goog authored Jan 10, 2025
2 parents 2335f93 + 2a72f90 commit 293a316
Show file tree
Hide file tree
Showing 27 changed files with 215 additions and 150 deletions.
2 changes: 1 addition & 1 deletion apis/bigquerydatatransfer/v1alpha1/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type BigQueryDataTransferConfigObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcpbigquerydatatransferconfig;gcpbigquerydatatransferconfigs
// +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"
Expand Down
2 changes: 1 addition & 1 deletion apis/bigquerydatatransfer/v1beta1/transferconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ type BigQueryDataTransferConfigObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcpbigquerydatatransferconfig;gcpbigquerydatatransferconfigs
// +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"
Expand Down
1 change: 1 addition & 0 deletions apis/cloudbuild/v1alpha1/workerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type NetworkConfigState struct {
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool API
// +kubebuilder:subresource:status
// +kubebuilder:resource:categories=gcp,shortName=gcpcloudbuildworkerpool;gcpcloudbuildworkerpools
type CloudBuildWorkerPool struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions apis/cloudbuild/v1beta1/workerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type CloudBuildWorkerPoolObservedState struct {
// CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool API
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:resource:categories=gcp,shortName=gcpcloudbuildworkerpool;gcpcloudbuildworkerpools
type CloudBuildWorkerPool struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion apis/firestore/v1alpha1/database_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type FirestoreDatabaseObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcpfirestoredatabase;gcpfirestoredatabases
// +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"
Expand Down
2 changes: 1 addition & 1 deletion apis/firestore/v1beta1/database_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type FirestoreDatabaseObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcpfirestoredatabase;gcpfirestoredatabases
// +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"
Expand Down
80 changes: 80 additions & 0 deletions apis/kms/v1beta1/autokeyconfig_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 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 v1beta1

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"
)

type KMSAutokeyConfigIdentity struct {
parent *KMSAutokeyConfigParent
}

func (i *KMSAutokeyConfigIdentity) String() string {
return i.parent.String() + "/autokeyConfig"
}

func (r *KMSAutokeyConfigIdentity) Parent() *KMSAutokeyConfigParent {
return r.parent
}

type KMSAutokeyConfigParent struct {
FolderID string
}

func (p *KMSAutokeyConfigParent) String() string {
return "folders/" + p.FolderID
}

func NewAutokeyConfigIdentity(ctx context.Context, reader client.Reader, obj *KMSAutokeyConfig) (*KMSAutokeyConfigIdentity, error) {
// Get Parent
folderRef, err := refsv1beta1.ResolveFolder(ctx, reader, obj, obj.Spec.FolderRef)

if err != nil {
return nil, err
}
folderID := folderRef.FolderID
externalRef := common.ValueOf(obj.Status.ExternalRef)
if externalRef != "" {
actualIdentity, err := ParseKMSAutokeyConfigExternal(externalRef)
if err != nil {
return nil, err
}
if actualIdentity.parent.FolderID != folderID {
return nil, fmt.Errorf("spec.folderRef changed, expect %s, got %s", actualIdentity.parent.FolderID, folderID)
}
}

return &KMSAutokeyConfigIdentity{
parent: &KMSAutokeyConfigParent{FolderID: folderID},
}, nil
}

func ParseKMSAutokeyConfigExternal(external string) (parent *KMSAutokeyConfigIdentity, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 3 || tokens[0] != "folders" || tokens[2] != "autokeyConfig" {
return nil, fmt.Errorf("format of KMSAutokeyConfig external=%q was not known (use folders/<folderID>/autokeyConfig)", external)
}
return &KMSAutokeyConfigIdentity{parent: &KMSAutokeyConfigParent{
FolderID: tokens[1],
}}, nil
}
76 changes: 0 additions & 76 deletions apis/kms/v1beta1/autokeyconfig_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1beta1
import (
"context"
"fmt"
"strings"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
Expand All @@ -41,8 +40,6 @@ type KMSAutokeyConfigRef struct {

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

parent *KMSAutokeyConfigParent
}

// NormalizedExternal provision the "External" value for other resource that depends on KMSAutokeyConfig.
Expand Down Expand Up @@ -85,79 +82,6 @@ func (r *KMSAutokeyConfigRef) NormalizedExternal(ctx context.Context, reader cli
return r.External, nil
}

// New builds a KMSAutokeyConfigRef from the Config Connector KMSAutokeyConfig object.
func NewKMSAutokeyConfigRef(ctx context.Context, reader client.Reader, obj *KMSAutokeyConfig) (*KMSAutokeyConfigRef, error) {
id := &KMSAutokeyConfigRef{}

// Get Parent
folderRef, err := refsv1beta1.ResolveFolder(ctx, reader, obj, obj.Spec.FolderRef)
if err != nil {
return nil, err
}
folderID := folderRef.FolderID
if folderID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
id.parent = &KMSAutokeyConfigParent{FolderID: folderID}

// Use approved External
externalRef := valueOf(obj.Status.ExternalRef)
if externalRef == "" {
id.External = AsKMSAutokeyConfigExternal(id.parent)
return id, nil
}

// Validate desired with actual
actualParent, err := ParseKMSAutokeyConfigExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.FolderID != folderID {
return nil, fmt.Errorf("spec.folderRef changed, expect %s, got %s", actualParent.FolderID, folderID)
}
id.External = externalRef
id.parent = &KMSAutokeyConfigParent{FolderID: folderID}
return id, nil
}

func (r *KMSAutokeyConfigRef) Parent() (*KMSAutokeyConfigParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, err := ParseKMSAutokeyConfigExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("KMSAutokeyConfigRef not initialized from `NewKMSAutokeyConfigRef` or `NormalizedExternal`")
}

type KMSAutokeyConfigParent struct {
FolderID string
}

func (p *KMSAutokeyConfigParent) String() string {
return "folders/" + p.FolderID
}

func AsKMSAutokeyConfigExternal(parent *KMSAutokeyConfigParent) (external string) {
return parent.String() + "/autokeyConfig"
}

func ParseKMSAutokeyConfigExternal(external string) (parent *KMSAutokeyConfigParent, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 3 || tokens[0] != "folders" || tokens[2] != "autokeyConfig" {
return nil, fmt.Errorf("format of KMSAutokeyConfig external=%q was not known (use folders/<folderID>/autokeyConfig)", external)
}
parent = &KMSAutokeyConfigParent{
FolderID: tokens[1],
}
return parent, nil
}

func valueOf[T any](t *T) T {
var zeroVal T
if t == nil {
Expand Down
25 changes: 20 additions & 5 deletions apis/kms/v1beta1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion apis/redis/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type RedisClusterObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcprediscluster;gcpredisclusters
// +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"
Expand Down
2 changes: 1 addition & 1 deletion apis/redis/v1beta1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type RedisClusterObservedState struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:resource:categories=gcp,shortName=gcprediscluster;gcpredisclusters
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/system=true";"cnrm.cloud.google.com/stability-level=beta"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ spec:
kind: BigQueryDataTransferConfig
listKind: BigQueryDataTransferConfigList
plural: bigquerydatatransferconfigs
shortNames:
- gcpbigquerydatatransferconfig
- gcpbigquerydatatransferconfigs
singular: bigquerydatatransferconfig
preserveUnknownFields: false
scope: Namespaced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ metadata:
spec:
group: cloudbuild.cnrm.cloud.google.com
names:
categories:
- gcp
kind: CloudBuildWorkerPool
listKind: CloudBuildWorkerPoolList
plural: cloudbuildworkerpools
shortNames:
- gcpcloudbuildworkerpool
- gcpcloudbuildworkerpools
singular: cloudbuildworkerpool
preserveUnknownFields: false
scope: Namespaced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ spec:
kind: FirestoreDatabase
listKind: FirestoreDatabaseList
plural: firestoredatabases
shortNames:
- gcpfirestoredatabase
- gcpfirestoredatabases
singular: firestoredatabase
preserveUnknownFields: false
scope: Namespaced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ spec:
kind: RedisCluster
listKind: RedisClusterList
plural: redisclusters
shortNames:
- gcprediscluster
- gcpredisclusters
singular: rediscluster
preserveUnknownFields: false
scope: Namespaced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package generatecontroller

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -96,5 +97,10 @@ func RunController(ctx context.Context, o *GenerateControllerOptions) error {
ProtoResource: o.ProtoName,
ProtoVersion: version,
}
return scaffold.Scaffold(serviceName, o.ProtoName, cArgs)
err1 := scaffold.GenerateController(serviceName, o.ProtoName, cArgs)
err2 := scaffold.RegisterController(serviceName, o.ProtoName)
if err1 != nil || err2 != nil {
return errors.Join(err1, err2)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ func RunGenerateBasicReconciler(ctx context.Context, o *GenerateBasicReconcilerO
OutputMapperDirectory: o.OutputMapperDirectory,
}
if err := generatemapper.RunGenerateMapper(ctx, mapperOps); err != nil {
return fmt.Errorf("generate types: %w", err)
return fmt.Errorf("generate mapper: %w", err)
}
controllerOps := &generatecontroller.GenerateControllerOptions{
GenerateOptions: o.GenerateOptions,
Kind: o.Kind,
ProtoName: o.ProtoName,
}
if err := generatecontroller.RunController(ctx, controllerOps); err != nil {
return fmt.Errorf("generate types: %w", err)
return fmt.Errorf("generate controller: %w", err)
}
return nil
}
Loading

0 comments on commit 293a316

Please sign in to comment.