func()

in controllers/cnimanager/server.go [38:74]


func (s *NicService) NicAdd(ctx context.Context, in *cniprotocol.NicAddRequest) (*cniprotocol.NicAddResponse, error) {
	gwConfig := &current.StaticGatewayConfiguration{}
	if err := s.k8sClient.Get(ctx, client.ObjectKey{Name: in.GetGatewayName(), Namespace: in.GetPodConfig().GetPodNamespace()}, gwConfig); err != nil {
		return nil, status.Errorf(codes.Unknown, "failed to retrieve StaticGatewayConfiguration %s/%s: %s", in.GetPodConfig().GetPodNamespace(), in.GetGatewayName(), err)
	}
	if len(gwConfig.Status.EgressIpPrefix) == 0 {
		return nil, status.Errorf(codes.FailedPrecondition, "the egress IP prefix is not ready yet.")
	}
	pod := &corev1.Pod{}
	if err := s.k8sClient.Get(ctx, client.ObjectKey{Name: in.GetPodConfig().GetPodName(), Namespace: in.GetPodConfig().GetPodNamespace()}, pod); err != nil {
		return nil, status.Errorf(codes.Unknown, "failed to retrieve pod %s/%s: %s", in.GetPodConfig().GetPodNamespace(), in.GetPodConfig().GetPodName(), err)
	}
	podEndpoint := &current.PodEndpoint{ObjectMeta: metav1.ObjectMeta{Name: in.GetPodConfig().GetPodName(), Namespace: in.GetPodConfig().GetPodNamespace()}}
	if _, err := controllerutil.CreateOrUpdate(ctx, s.k8sClient, podEndpoint, func() error {
		if err := controllerutil.SetControllerReference(pod, podEndpoint, s.k8sClient.Scheme()); err != nil {
			return err
		}
		podEndpoint.Spec.PodIpAddress = in.GetAllowedIp()
		podEndpoint.Spec.StaticGatewayConfiguration = in.GetGatewayName()
		podEndpoint.Spec.PodPublicKey = in.PublicKey
		return nil
	}); err != nil {
		return nil, status.Errorf(codes.Unknown, "failed to update PodEndpoint %s/%s: %s", in.GetPodConfig().GetPodNamespace(), in.GetPodConfig().GetPodName(), err)
	}

	defaultRoute := cniprotocol.DefaultRoute_DEFAULT_ROUTE_STATIC_EGRESS_GATEWAY
	if gwConfig.Spec.DefaultRoute == current.RouteAzureNetworking {
		defaultRoute = cniprotocol.DefaultRoute_DEFAULT_ROUTE_AZURE_NETWORKING
	}
	return &cniprotocol.NicAddResponse{
		EndpointIp:     gwConfig.Status.Ip,
		ListenPort:     gwConfig.Status.Port,
		PublicKey:      gwConfig.Status.PublicKey,
		ExceptionCidrs: gwConfig.Spec.ExcludeCidrs,
		DefaultRoute:   defaultRoute,
	}, nil
}