Skip to content

Commit

Permalink
PCP-1159: added default function and minor lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamrajvanshi committed Jan 19, 2024
1 parent 82f0ba3 commit 2bd5780
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 695 deletions.
678 changes: 0 additions & 678 deletions api/v1beta1/azuremanagedcontrolplane_webhook.go

This file was deleted.

6 changes: 6 additions & 0 deletions azure/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const (
DefaultUserName = "capi"
// DefaultAKSUserName is the default username for a created AKS VM.
DefaultAKSUserName = "azureuser"
// PublicCloudName is the name of the Azure public cloud.
PublicCloudName = "AzurePublicCloud"
// ChinaCloudName is the name of the Azure China cloud.
ChinaCloudName = "AzureChinaCloud"
// USGovernmentCloudName is the name of the Azure US Government cloud.
USGovernmentCloudName = "AzureUSGovernmentCloud"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,6 @@ spec:
- None
type: string
type: object
azureEnvironment:
description: 'AzureEnvironment is the name of the AzureCloud to be
used. The default value that would be used by most users is "AzurePublicCloud",
other values are: - ChinaCloud: "AzureChinaCloud" - PublicCloud:
"AzurePublicCloud" - USGovernmentCloud: "AzureUSGovernmentCloud"'
type: string
controlPlaneEndpoint:
description: ControlPlaneEndpoint represents the endpoint used to
communicate with the control plane.
Expand Down Expand Up @@ -599,6 +593,12 @@ spec:
required:
- upgradeChannel
type: object
azureEnvironment:
description: 'AzureEnvironment is the name of the AzureCloud to be
used. The default value that would be used by most users is "AzurePublicCloud",
other values are: - ChinaCloud: "AzureChinaCloud" - PublicCloud:
"AzurePublicCloud" - USGovernmentCloud: "AzureUSGovernmentCloud"'
type: string
controlPlaneEndpoint:
description: ControlPlaneEndpoint represents the endpoint used to
communicate with the control plane.
Expand Down
2 changes: 1 addition & 1 deletion config/default/manager_image_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spec:
spec:
containers:
# Change the value of image field below to your controller image URL
- image: gcr.io/spectro-dev-public/ubuntu/release/cluster-api-azure-controller:v1.3.2-spectro-4.0.0-dev
- image: gcr.io/spectro-dev-public/shubham/release/cluster-api-azure-controller:v1.3.2-spectro-4.0.0-dev
name: manager
1 change: 1 addition & 0 deletions exp/api/v1alpha3/zz_generated.conversion.go

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

1 change: 1 addition & 0 deletions exp/api/v1alpha4/zz_generated.conversion.go

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

2 changes: 1 addition & 1 deletion exp/api/v1beta1/azuremanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type AzureManagedControlPlaneSpec struct {
// AutoUpgradeProfile - Profile of auto upgrade configuration.
// +optional
AutoUpgradeProfile *ManagedClusterAutoUpgradeProfile `json:"autoUpgradeProfile,omitempty"`

// AzureEnvironment is the name of the AzureCloud to be used.
// The default value that would be used by most users is "AzurePublicCloud", other values are:
// - ChinaCloud: "AzureChinaCloud"
Expand Down
23 changes: 23 additions & 0 deletions exp/api/v1beta1/azuremanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/versions"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -77,6 +78,9 @@ func (m *AzureManagedControlPlane) Default(_ client.Client) {
m.setDefaultVirtualNetwork()
m.setDefaultSubnet()
m.setDefaultSku()
if m.Spec.AzureEnvironment == "" {
m.Spec.AzureEnvironment = azure.PublicCloudName
}
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplane,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanes,versions=v1beta1,name=validation.azuremanagedcontrolplanes.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
Expand Down Expand Up @@ -253,6 +257,25 @@ func (m *AzureManagedControlPlane) ValidateUpdate(oldRaw runtime.Object, client
}
}

if old.Spec.AzureEnvironment != "" {
// Prevent AzureEnvironment modification if it was already set to some value
if m.Spec.AzureEnvironment == "" {
// unsetting the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "AzureEnvironment"),
m.Spec.AzureEnvironment,
"field is immutable, unsetting is not allowed"))
} else if m.Spec.AzureEnvironment != old.Spec.AzureEnvironment {
// changing the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "AzureEnvironment"),
*m.Spec.LoadBalancerSKU,
"field is immutable"))
}
}

if m.Spec.DisableLocalAccounts != nil &&
m.Spec.AADProfile == nil {
allErrs = append(allErrs,
Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package main

import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"time"
"crypto/tls"

// +kubebuilder:scaffold:imports
aadpodv1 "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity/v1"
Expand All @@ -35,6 +35,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/leaderelection/resourcelock"
cgrecord "k8s.io/client-go/tools/record"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
infrav1alpha3 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
Expand All @@ -60,7 +61,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
cliflag "k8s.io/component-base/cli/flag"
)

type TLSOptions struct {
Expand All @@ -69,8 +69,8 @@ type TLSOptions struct {
}

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
tlsOptions = TLSOptions{}
)

Expand Down Expand Up @@ -277,10 +277,10 @@ func main() {
})

tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsOptions)
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}

restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = "cluster-api-provider-azure-manager"
Expand All @@ -299,7 +299,7 @@ func main() {
HealthProbeBindAddress: healthAddr,
Port: webhookPort,
EventBroadcaster: broadcaster,
TLSOpts: tlsOptionOverrides,
TLSOpts: tlsOptionOverrides,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit 2bd5780

Please sign in to comment.