Skip to content

Commit

Permalink
ResourceGroup needs defaulting webhook (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthchr authored Jan 7, 2021
1 parent 7dfd34d commit b2dbc75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ tasks:
- yq read -c {{.IN}} 'webhooks.(rules[*].resources[*]. == batchaccounts)' | yq prefix - webhooks | yq merge -a append - {{.OUT}} > {{.OUT}}_2 && mv {{.OUT}}_2 {{.OUT}}
- yq read -c {{.IN}} 'webhooks.(rules[*].resources[*]. == storageaccounts)' | yq prefix - webhooks | yq merge -a append - {{.OUT}} > {{.OUT}}_2 && mv {{.OUT}}_2 {{.OUT}}
- yq read -c {{.IN}} 'webhooks.(rules[*].resources[*]. == storageaccountsblobservicescontainers)' | yq prefix - webhooks | yq merge -a append - {{.OUT}} > {{.OUT}}_2 && mv {{.OUT}}_2 {{.OUT}}
- yq read -c {{.IN}} 'webhooks.(rules[*].resources[*]. == resourcegroups)' | yq prefix - webhooks | yq merge -a append - {{.OUT}} > {{.OUT}}_2 && mv {{.OUT}}_2 {{.OUT}}
# need to remove v1 from the manifest as it doesn't work with controller-runtime currently: https://github.com/kubernetes-sigs/controller-runtime/issues/1272
- yq delete -i {{.OUT}} 'webhooks[*].admissionReviewVersions(. == v1)'
- yq delete -P -i {{.OUT}} 'webhooks[*].admissionReviewVersions(. == v1)'
vars:
IN: config/webhook/manifests.yaml
OUT: config/webhook/valid/manifests.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/Azure/k8s-infra/hack/generated/pkg/genruntime"
)
Expand All @@ -20,6 +21,17 @@ type ResourceGroup struct {
Status ResourceGroupStatus `json:"status,omitempty"`
}

// +kubebuilder:webhook:path=/mutate-microsoft-resources-infra-azure-com-v20200601-resourcegroup,mutating=true,sideEffects=None,matchPolicy=Exact,failurePolicy=fail,groups=microsoft.resources.infra.azure.com,resources=resourcegroups,verbs=create;update,versions=v20200601,name=default.v20200601.resourcegroups.microsoft.resources.infra.azure.com

var _ admission.Defaulter = &ResourceGroup{}

// Default defaults the Azure name of the resource to the Kubernetes name
func (rg *ResourceGroup) Default() {
if rg.Spec.AzureName == "" {
rg.Spec.AzureName = rg.Name
}
}

var _ genruntime.KubernetesResource = &ResourceGroup{}

// AzureName returns the Azure name of the resource
Expand Down Expand Up @@ -185,13 +197,16 @@ func (spec *ResourceGroupSpec) PopulateFromArm(owner genruntime.KnownResourceRef
return fmt.Errorf("unexpected type supplied for PopulateFromArm() function. Expected ResourceGroupSpecArm, got %T", armInput)
}
// spec.ApiVersion = typedInput.ApiVersion
spec.AzureName = genruntime.ExtractKubernetesResourceNameFromArmName(typedInput.Name)
spec.SetAzureName(genruntime.ExtractKubernetesResourceNameFromArmName(typedInput.Name))
spec.Location = typedInput.Location
spec.ManagedBy = typedInput.ManagedBy
spec.Tags = typedInput.Tags
return nil
}

// SetAzureName sets the Azure name of the resource
func (spec *ResourceGroupSpec) SetAzureName(azureName string) { spec.AzureName = azureName }

func init() {
SchemeBuilder.Register(&ResourceGroup{}, &ResourceGroupList{})
}

0 comments on commit b2dbc75

Please sign in to comment.