Skip to content

Commit

Permalink
feat(GcpNfsVolume): make location optional
Browse files Browse the repository at this point in the history
  • Loading branch information
abalaie committed Jul 19, 2024
1 parent b660db2 commit 99a5c8c
Show file tree
Hide file tree
Showing 23 changed files with 288 additions and 18 deletions.
7 changes: 7 additions & 0 deletions api/cloud-control/v1beta1/scope_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ type GcpScope struct {

// +optional
Network GcpNetwork `json:"network"`

// +optional
Workers []GcpWorkers `json:"workers"`
}

type GcpWorkers struct {
// +kubebuilder:validation:Required
Zones []string `json:"zones"`
}
type GcpNetwork struct {
// +optional
Nodes string `json:"nodes,omitempty"`
Expand Down
29 changes: 28 additions & 1 deletion api/cloud-control/v1beta1/zz_generated.deepcopy.go

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

6 changes: 5 additions & 1 deletion api/cloud-resources/v1beta1/gcpnfsvolume_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ const (
ConditionReasonPVNotReadyForNameChange = "PVNotReadyForNameChange"
ConditionReasonPVNameInvalid = "PVNameInvalid"
ConditionReasonPVCNameInvalid = "PVCNameInvalid"
ConditionReasonNoWorkerZones = "NoWorkerZones"
ConditionReasonLocationInvalid = "LocationInvalid"
)

// GcpNfsVolumeSpec defines the desired state of GcpNfsVolume
type GcpNfsVolumeSpec struct {
// +optional
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="IpRange is immutable."
IpRange IpRangeRef `json:"ipRange"`
// +kubebuilder:validation:Required
// +optional
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="Location is immutable."
Location string `json:"location"`

Expand Down Expand Up @@ -119,6 +121,8 @@ type GcpNfsVolumeStatus struct {

//State of the GcpNfsVolume
State GcpNfsVolumeState `json:"state,omitempty"`

Location string `json:"location,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
11 changes: 11 additions & 0 deletions config/crd/bases/cloud-control.kyma-project.io_scopes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ spec:
type: string
vpcNetwork:
type: string
workers:
items:
properties:
zones:
items:
type: string
type: array
required:
- zones
type: object
type: array
required:
- project
- vpcNetwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
cloud-resources.kyma-project.io/version: v0.0.2
cloud-resources.kyma-project.io/version: v0.0.3
name: gcpnfsvolumes.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -114,7 +114,6 @@ spec:
required:
- capacityGb
- fileShareName
- location
- tier
type: object
status:
Expand Down Expand Up @@ -177,6 +176,8 @@ spec:
type: array
id:
type: string
location:
type: string
state:
description: State of the GcpNfsVolume
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ spec:
type: string
vpcNetwork:
type: string
workers:
items:
properties:
zones:
items:
type: string
type: array
required:
- zones
type: object
type: array
required:
- project
- vpcNetwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
cloud-resources.kyma-project.io/version: v0.0.2
cloud-resources.kyma-project.io/version: v0.0.3
name: gcpnfsvolumes.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -114,7 +114,6 @@ spec:
required:
- capacityGb
- fileShareName
- location
- tier
type: object
status:
Expand Down Expand Up @@ -177,6 +176,8 @@ spec:
type: array
id:
type: string
location:
type: string
state:
description: State of the GcpNfsVolume
type: string
Expand Down
2 changes: 1 addition & 1 deletion config/patchAfterMakeManifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ echo "Patching CRDs..."

yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.1.0"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_ipranges.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.2"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsnfsvolumes.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.2"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml
13 changes: 13 additions & 0 deletions pkg/feature/ffGcpNfsVolumeAutomaticLocationAllocation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package feature

import "context"

const gcpNfsVolumeAutomaticLocationAllocationFlagName = "gcpNfsVolumeAutomaticLocationAllocation"

var GcpNfsVolumeAutomaticLocationAllocation = &gcpNfsVolumeAutomaticLocationAllocationInfo{}

type gcpNfsVolumeAutomaticLocationAllocationInfo struct{}

func (k *gcpNfsVolumeAutomaticLocationAllocationInfo) Value(ctx context.Context) bool {
return provider.BoolVariation(ctx, gcpNfsVolumeAutomaticLocationAllocationFlagName, false)
}
1 change: 1 addition & 0 deletions pkg/kcp/scope/createScope.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func createScope(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)

switch state.provider {
case cloudcontrolv1beta1.ProviderGCP:
return createScopeGcp(ctx, state)
Expand Down
4 changes: 4 additions & 0 deletions pkg/kcp/scope/createScopeAws.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func createScopeAws(ctx context.Context, st composed.State) (error, context.Cont
},
}

// Preserve loaded obj resource version before getting overwritten by newly created scope
if st.Obj() != nil && st.Obj().GetName() != "" {
scope.ResourceVersion = st.Obj().GetResourceVersion()
}
state.SetObj(scope)

return nil, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/kcp/scope/createScopeAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func createScopeAzure(ctx context.Context, st composed.State) (error, context.Co
},
}

// Preserve loaded obj resource version before getting overwritten by newly created scope
if st.Obj() != nil && st.Obj().GetName() != "" {
scope.ResourceVersion = st.Obj().GetResourceVersion()
}
state.SetObj(scope)

return nil, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/kcp/scope/createScopeGcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/elliotchance/pie/v2"
gardenerv1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"github.com/kyma-project/cloud-manager/pkg/composed"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -48,11 +50,20 @@ func createScopeGcp(ctx context.Context, st composed.State) (error, context.Cont
Pods: ptr.Deref(state.shoot.Spec.Networking.Pods, ""),
Services: ptr.Deref(state.shoot.Spec.Networking.Services, ""),
},
Workers: pie.Map(state.shoot.Spec.Provider.Workers, func(w gardenerv1beta1.Worker) cloudcontrolv1beta1.GcpWorkers {
return cloudcontrolv1beta1.GcpWorkers{
Zones: w.Zones,
}
}),
},
},
},
}

// Preserve loaded obj resource version before getting overwritten by newly created scope
if st.Obj() != nil && st.Obj().GetName() != "" {
scope.ResourceVersion = st.Obj().GetResourceVersion()
}
state.SetObj(scope)

return nil, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/kcp/scope/loadScopeObj.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ func loadScopeObj(ctx context.Context, st composed.State) (error, context.Contex
if err != nil {
return composed.LogErrorAndReturn(err, "Error loading Scope object", composed.StopWithRequeue, ctx)
}

return nil, nil
}
24 changes: 21 additions & 3 deletions pkg/kcp/scope/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scope

import (
"context"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"github.com/kyma-project/cloud-manager/pkg/composed"
awsclient "github.com/kyma-project/cloud-manager/pkg/kcp/provider/aws/client"
gcpclient "github.com/kyma-project/cloud-manager/pkg/kcp/provider/gcp/client"
Expand Down Expand Up @@ -99,15 +100,15 @@ func (r *scopeReconciler) newAction() composed.Action {
addKymaFinalizer,
composed.BuildBranchingAction(
"scopeAlreadyCreatedBranching",
ObjIsLoadedPredicate(),
composed.ComposeActions( // This is called only if scope exits.
composed.All(ObjIsLoadedPredicate(), composed.Not(UpdateIsNeededPredicate())),
composed.ComposeActions( // This is called only if scope exits, and it does not need to be updated.
"enableApisAndActivateSkr",
enableApis,
addReadyCondition,
skrActivate,
composed.StopAndForgetAction),
nil),
// scope does not exist
// scope does not exist or needs to be updated
createGardenerClient,
findShootName,
loadShoot,
Expand All @@ -128,3 +129,20 @@ func ObjIsLoadedPredicate() composed.Predicate {
return obj != nil && obj.GetName() != "" // empty object is created when state gets created
}
}

func UpdateIsNeededPredicate() composed.Predicate {
return func(ctx context.Context, st composed.State) bool {
if ObjIsLoadedPredicate()(ctx, st) {
state := st.(*State)
scope := state.ObjAsScope().Spec
switch scope.Provider {
case cloudcontrolv1beta1.ProviderGCP:
return scope.Scope.Gcp.Workers == nil || len(scope.Scope.Gcp.Workers) == 0
default:
return false
}
} else {
return false
}
}
}
18 changes: 14 additions & 4 deletions pkg/kcp/scope/saveScope.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import (
func saveScope(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)

err := state.Cluster().K8sClient().Create(ctx, state.ObjAsScope())
if err != nil {
return composed.LogErrorAndReturn(err, "Error creating scope", composed.StopWithRequeue, ctx)
if state.Obj().GetResourceVersion() != "" {
// Existing scope, update it
err := state.Cluster().K8sClient().Update(ctx, state.ObjAsScope())
if err != nil {
return composed.LogErrorAndReturn(err, "Error updating scope", composed.StopWithRequeue, ctx)
}
composed.LoggerFromCtx(ctx).Info("Scope updated")
} else {
// New scope, create it
err := state.Cluster().K8sClient().Create(ctx, state.ObjAsScope())
if err != nil {
return composed.LogErrorAndReturn(err, "Error creating scope", composed.StopWithRequeue, ctx)
}
composed.LoggerFromCtx(ctx).Info("Scope created")
}
composed.LoggerFromCtx(ctx).Info("Scope created")

return nil, nil
}
Loading

0 comments on commit 99a5c8c

Please sign in to comment.