Skip to content

Commit

Permalink
Add checks and OperatorSpec property name refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
super-harsh committed Jun 21, 2024
1 parent 0705899 commit e9cfde5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 35 deletions.
44 changes: 27 additions & 17 deletions v2/api/network/customizations/private_endpoints_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,41 @@ func (extension *PrivateEndpointExtension) ExportKubernetesResources(
// the hub type has been changed but this extension has not
var _ conversion.Hub = endpoint

hasIpConfiguration := configMapSpecified(endpoint)
hasIpConfiguration := hasConfigMaps(endpoint)
if !hasIpConfiguration {
log.V(Debug).Info("no configmap retrieval to perform as operatorSpec is empty")
return nil, nil
}

if endpoint.Status.NetworkInterfaces == nil || len(endpoint.Status.NetworkInterfaces) == 0 {
log.V(Debug).Info("no configmap retrieval to perform as there is no NetworkInterfaces attached")
log.V(Debug).Info("no configmap retrieval to perform as there are no NetworkInterfaces attached")
return nil, nil
}

nicId, err := arm.ParseResourceID(*endpoint.Status.NetworkInterfaces[0].Id)
if endpoint.Status.NetworkInterfaces[0].Id == nil {
log.V(Debug).Info("no configmap retrieval to perform, failed to fetch the attached NetworkInterfaces")
return nil, nil
}

nicID, err := arm.ParseResourceID(*endpoint.Status.NetworkInterfaces[0].Id)
if err != nil {
return nil, err
}

// The default primary ip configuration for PrivateEndpoint is on NetworkInterfaceController. Hence, we fetch it from there.
var interfacesClient *armnetwork.InterfacesClient
interfacesClient, err = armnetwork.NewInterfacesClient(nicId.SubscriptionID, armClient.Creds(), armClient.ClientOptions())
interfacesClient, err = armnetwork.NewInterfacesClient(nicID.SubscriptionID, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return nil, errors.Wrapf(err, "failed to create new PrivateEndpointsClient")
return nil, errors.Wrapf(err, "failed to create new NetworkInterfacesClient")
}

var resp armnetwork.InterfacesClientGetResponse
resp, err = interfacesClient.Get(ctx, nicId.ResourceGroupName, nicId.Name, nil)
resp, err = interfacesClient.Get(ctx, nicID.ResourceGroupName, nicID.Name, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed getting PrivateEndpoint")
return nil, errors.Wrapf(err, "failed getting NetworkInterfaceController")
}

configsByName := configByName(resp)
configsByName := configByName(resp.Interface)
configs, err := configMapToWrite(endpoint, configsByName)
if err != nil {
return nil, err
Expand All @@ -123,11 +128,16 @@ func (extension *PrivateEndpointExtension) ExportKubernetesResources(
return configmaps.SliceToClientObjectSlice(configs), nil
}

func configByName(resp armnetwork.InterfacesClientGetResponse) map[string]string {
func configByName(resp armnetwork.Interface) map[string]string {
result := make(map[string]string)

if resp.Properties != nil && resp.Properties.IPConfigurations != nil && len(resp.Properties.IPConfigurations) > 0 {
result["PrimaryNicPrivateIPAddress"] = *resp.Properties.IPConfigurations[0].Properties.PrivateIPAddress
if resp.Properties != nil &&
resp.Properties.IPConfigurations != nil &&
len(resp.Properties.IPConfigurations) > 0 &&
resp.Properties.IPConfigurations[0].Properties != nil &&
resp.Properties.IPConfigurations[0].Properties.PrivateIPAddress != nil {

result["primaryNICPrivateIPAddress"] = *resp.Properties.IPConfigurations[0].Properties.PrivateIPAddress
}

return result
Expand All @@ -141,25 +151,25 @@ func configMapToWrite(obj *network.PrivateEndpoint, configs map[string]string) (

collector := configmaps.NewCollector(obj.Namespace)

primaryNicPrivateIPAddress, ok := configs["PrimaryNicPrivateIPAddress"]
primaryNICPrivateIPAddress, ok := configs["primaryNICPrivateIPAddress"]
if ok {
collector.AddValue(operatorSpecConfigs.PrimaryNicPrivateIPAddress, primaryNicPrivateIPAddress)
collector.AddValue(operatorSpecConfigs.PrimaryNicPrivateIpAddress, primaryNICPrivateIPAddress)
}

return collector.Values()
}

func configMapSpecified(endpoint *network.PrivateEndpoint) bool {
func hasConfigMaps(endpoint *network.PrivateEndpoint) bool {
if endpoint.Spec.OperatorSpec == nil || endpoint.Spec.OperatorSpec.ConfigMaps == nil {
return false
}

hasIpConfiguration := false
hasIPConfiguration := false
configMaps := endpoint.Spec.OperatorSpec.ConfigMaps

if configMaps != nil && configMaps.PrimaryNicPrivateIPAddress != nil {
hasIpConfiguration = true
hasIPConfiguration = true
}

return hasIpConfiguration
return hasIPConfiguration
}
26 changes: 13 additions & 13 deletions v2/api/network/v1api20220701/private_endpoint_types_gen.go

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

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

2 changes: 1 addition & 1 deletion v2/api/network/v1api20220701/storage/structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ PrivateEndpoint: Resource
│ │ └── RequestMessage: *string
│ ├── OperatorSpec: *Object (2 properties)
│ │ ├── ConfigMaps: *Object (2 properties)
│ │ │ ├── PrimaryNicPrivateIPAddress: *genruntime.ConfigMapDestination
│ │ │ ├── PrimaryNicPrivateIpAddress: *genruntime.ConfigMapDestination
│ │ │ └── PropertyBag: genruntime.PropertyBag
│ │ └── PropertyBag: genruntime.PropertyBag
│ ├── OriginalVersion: string
Expand Down
2 changes: 1 addition & 1 deletion v2/api/network/v1api20220701/structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,7 @@ PrivateEndpoint: Resource
│ │ └── RequestMessage: *string
│ ├── OperatorSpec: *Object (1 property)
│ │ └── ConfigMaps: *Object (1 property)
│ │ └── PrimaryNicPrivateIPAddress: *genruntime.ConfigMapDestination
│ │ └── PrimaryNicPrivateIpAddress: *genruntime.ConfigMapDestination
│ ├── Owner: *genruntime.KnownResourceReference
│ ├── PrivateLinkServiceConnections: Object (5 properties)[]
│ │ ├── GroupIds: string[]
Expand Down
2 changes: 1 addition & 1 deletion v2/azure-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ objectModelConfiguration:
$export: true
$supportedFrom: v2.0.0
$manualConfigs:
- PrimaryNicPrivateIPAddress
- PrimaryNicPrivateIpAddress
PrivateLinkService:
$export: true
$supportedFrom: v2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_Networking_PrivateEndpoint_CRUD(t *testing.T) {
endpoint := newPrivateEndpoint(tc, rg, sa, subnet)
endpoint.Spec.OperatorSpec = &network.PrivateEndpointOperatorSpec{
ConfigMaps: &network.PrivateEndpointOperatorConfigMaps{
PrimaryNicPrivateIPAddress: &genruntime.ConfigMapDestination{
PrimaryNicPrivateIpAddress: &genruntime.ConfigMapDestination{
Name: configMapName,
Key: configMapKey,
},
Expand Down

0 comments on commit e9cfde5

Please sign in to comment.