func()

in pkg/controllers/member/serviceexport/controller.go [338:402]


func (r *Reconciler) setAzureRelatedInformation(ctx context.Context,
	service *corev1.Service,
	hubSvcExport *fleetnetv1alpha1.InternalServiceExport) error {
	hubSvcExport.Spec.Type = service.Spec.Type
	if service.Spec.Type != corev1.ServiceTypeLoadBalancer {
		return nil
	}
	// The annotation value is case-sensitive.
	// https://github.com/kubernetes-sigs/cloud-provider-azure/blob/release-1.31/pkg/provider/azure_loadbalancer.go#L3559
	hubSvcExport.Spec.IsInternalLoadBalancer = service.Annotations[objectmeta.ServiceAnnotationAzureLoadBalancerInternal] == "true"
	if hubSvcExport.Spec.IsInternalLoadBalancer {
		// no need to populate the PublicIPResourceID and IsDNSLabelConfigured which are only applicable for external load balancer
		return nil
	}

	serviceKObj := klog.KObj(service)
	if len(service.Status.LoadBalancer.Ingress) == 0 {
		// Assuming once the service status is updated, the controller will be triggered again.
		klog.V(2).InfoS("The load balancer IP is not assigned yet", "service", serviceKObj)
		return nil
	}

	if service.Status.LoadBalancer.Ingress[0].IP == "" {
		err := errors.New("the service ingress is not nil but with empty IP")
		klog.ErrorS(controller.NewUnexpectedBehaviorError(err), "Failed to get the load balancer IP from service", "service", serviceKObj, "status", service.Status)
		return nil
	}

	pip, err := r.lookupPublicIPResourceIDByLoadBalancerIP(ctx, service)
	if err != nil {
		return err
	}
	if pip == nil {
		klog.V(2).InfoS("The public IP is in the progressing", "service", serviceKObj, "ip", service.Status.LoadBalancer.Ingress[0].IP)
		// Assuming once the service status is updated, the controller will be triggered again in instead of retrying here
		// to avoid sending Azure requests.
		return nil
	}
	hubSvcExport.Spec.PublicIPResourceID = pip.ID

	// Note the user can set the dns label via the Azure portal or Azure CLI without updating service.
	// This information may be stale as we don't monitor the public IP address resource.
	hubSvcExport.Spec.IsDNSLabelConfigured = pip.Properties != nil && pip.Properties.DNSSettings != nil && pip.Properties.DNSSettings.DomainNameLabel != nil

	// No matter if the customer bring your own IP or not, the cloud provider will reconcile the DNS label based on the
	// DNS annotation.
	dnsName, found := service.Annotations[objectmeta.ServiceAnnotationAzureDNSLabelName]
	klog.V(2).InfoS("Finding whether the DNS is assigned", "service", serviceKObj, "dnsName", dnsName, "isSetOnService", found, "isConfiguredOnPIP", hubSvcExport.Spec.IsDNSLabelConfigured)
	// If the annotation is not set, the cloud provider won't reconcile the DNS label and return the current status.
	if !found {
		// cloud provider won't delete DNS label on pip if the annotation is not set.
		return nil
	}
	if len(dnsName) == 0 {
		hubSvcExport.Spec.IsDNSLabelConfigured = false // cloud provider will delete the DNS label on the pip.
		return nil
	}
	if !hubSvcExport.Spec.IsDNSLabelConfigured {
		err = fmt.Errorf("in the process of adding DNS to the public ip address %s", *pip.ID)
		klog.ErrorS(err, "Requeue the request to see if the DNS is ready or not", "service", serviceKObj)
		return err
	}

	return nil
}