|
| 1 | +package istio |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/go-logr/logr" |
| 7 | + kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1" |
| 8 | + "github.com/kubeflow/notebooks/workspaces/controller/internal/helper" |
| 9 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + ApiVersionIstio = "networking.istio.io/v1" |
| 17 | + VirtualServiceKind = "VirtualService" |
| 18 | + |
| 19 | + EnvIstioHost = "ISTIO_HOST" |
| 20 | + EnvIstioGateway = "ISTIO_GATEWAY" |
| 21 | + ClusterDomain = "CLUSTER_DOMAIN" |
| 22 | +) |
| 23 | + |
| 24 | +func GenerateIstioVirtualService(workspace *kubefloworgv1beta1.Workspace, workspaceKind *kubefloworgv1beta1.WorkspaceKind, imageConfig *kubefloworgv1beta1.ImageConfigValue, serviceName string, _ logr.Logger) (*unstructured.Unstructured, error) { |
| 25 | + |
| 26 | + virtualService := &unstructured.Unstructured{} |
| 27 | + virtualService.SetAPIVersion(ApiVersionIstio) |
| 28 | + virtualService.SetKind(VirtualServiceKind) |
| 29 | + |
| 30 | + prefix := helper.GenerateNamePrefix(workspace.Name, helper.MaxServiceNameLength) |
| 31 | + virtualService.SetName(helper.RemoveTrailingDash(prefix)) |
| 32 | + virtualService.SetNamespace(workspace.Namespace) |
| 33 | + |
| 34 | + // .spec.gateways |
| 35 | + istioGateway := helper.GetEnvOrDefault(EnvIstioGateway, "kubeflow/kubeflow-gateway") |
| 36 | + if err := unstructured.SetNestedStringSlice(virtualService.Object, []string{istioGateway}, |
| 37 | + "spec", "gateways"); err != nil { |
| 38 | + return nil, fmt.Errorf("set .spec.gateways error: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + istioHost := helper.GetEnvOrDefault(EnvIstioHost, "*") |
| 42 | + if err := unstructured.SetNestedStringSlice(virtualService.Object, []string{istioHost}, |
| 43 | + "spec", "gateways"); err != nil { |
| 44 | + return nil, fmt.Errorf("set .spec.hosts error: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + var prefixes []string |
| 48 | + for _, imagePort := range imageConfig.Spec.Ports { |
| 49 | + prefix := fmt.Sprintf("/workspace/%s/%s/%s", workspace.Namespace, workspace.Name, imagePort.Id) |
| 50 | + prefixes = append(prefixes, prefix) |
| 51 | + } |
| 52 | + |
| 53 | + var httpRoutes []interface{} |
| 54 | + |
| 55 | + host := fmt.Sprintf("%s.%s.svc.%s", serviceName, workspace.Namespace, helper.GetEnvOrDefault(ClusterDomain, "cluster.local")) |
| 56 | + |
| 57 | + // generate container ports |
| 58 | + // TODO: It can be better |
| 59 | + containerPortsIdMap, err := helper.GenerateContainerPortsIdMap(imageConfig) |
| 60 | + if errContainerPorts := unstructured.SetNestedStringSlice(virtualService.Object, []string{istioHost}, |
| 61 | + "spec", "gateways"); err != nil { |
| 62 | + return nil, fmt.Errorf("set .spec.hosts error: %v", errContainerPorts) |
| 63 | + } |
| 64 | + httpPathPrefixFunc := helper.GenerateHttpPathPrefixFunc(workspace, containerPortsIdMap) |
| 65 | + |
| 66 | + for _, imagePort := range imageConfig.Spec.Ports { |
| 67 | + |
| 68 | + httpRoute := map[string]interface{}{ |
| 69 | + "match": []map[string]interface{}{ |
| 70 | + { |
| 71 | + "uri": map[string]interface{}{ |
| 72 | + "prefix": fmt.Sprintf("/workspace/%s/%s/%s", workspace.Namespace, workspace.Name, imagePort.Id), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + "route": []map[string]interface{}{ |
| 77 | + { |
| 78 | + "destination": map[string]interface{}{ |
| 79 | + "host": host, |
| 80 | + "port": map[string]interface{}{ |
| 81 | + "number": imagePort.Port, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + if *workspaceKind.Spec.PodTemplate.HTTPProxy.RemovePathPrefix { |
| 89 | + httpRoute["rewrite"] = map[string]interface{}{"uri": "/"} |
| 90 | + } |
| 91 | + |
| 92 | + // templating.spec.http[].math.headers |
| 93 | + setHeaders := helper.TemplateHeaders(workspaceKind.Spec.PodTemplate.HTTPProxy.RequestHeaders.Set, httpPathPrefixFunc) |
| 94 | + addHeaders := helper.TemplateHeaders(workspaceKind.Spec.PodTemplate.HTTPProxy.RequestHeaders.Add, httpPathPrefixFunc) |
| 95 | + |
| 96 | + removeHeaders := make([]string, len(workspaceKind.Spec.PodTemplate.HTTPProxy.RequestHeaders.Remove)) |
| 97 | + for i, header := range workspaceKind.Spec.PodTemplate.HTTPProxy.RequestHeaders.Remove { |
| 98 | + if header != "" { |
| 99 | + out, err := helper.RenderValueUsingFunc(header, httpPathPrefixFunc) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to render header %q: %w", header, err) |
| 102 | + } |
| 103 | + header = out |
| 104 | + } |
| 105 | + removeHeaders[i] = header |
| 106 | + } |
| 107 | + |
| 108 | + httpRoute["headers"] = map[string]interface{}{ |
| 109 | + "request": map[string]interface{}{ |
| 110 | + "add": setHeaders, |
| 111 | + "set": addHeaders, |
| 112 | + "remove": removeHeaders, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + httpRoutes = append(httpRoutes, httpRoute) |
| 117 | + } |
| 118 | + |
| 119 | + virtualService.Object["spec"] = map[string]interface{}{ |
| 120 | + "gateways": []string{ |
| 121 | + istioGateway, |
| 122 | + }, |
| 123 | + "hosts": []string{ |
| 124 | + istioHost, |
| 125 | + }, |
| 126 | + "http": httpRoutes, |
| 127 | + } |
| 128 | + |
| 129 | + return virtualService, nil |
| 130 | +} |
| 131 | + |
| 132 | +func ReconcileVirtualService(ctx context.Context, r client.Client, virtualServiceName, namespace string, virtualService *unstructured.Unstructured, log logr.Logger) error { |
| 133 | + foundVirtualService := &unstructured.Unstructured{} |
| 134 | + foundVirtualService.SetAPIVersion(ApiVersionIstio) |
| 135 | + foundVirtualService.SetKind(VirtualServiceKind) |
| 136 | + justCreated := false |
| 137 | + if err := r.Get(ctx, types.NamespacedName{Name: virtualServiceName, Namespace: namespace}, foundVirtualService); err != nil { |
| 138 | + if apierrors.IsNotFound(err) { |
| 139 | + log.Info("Creating virtual service", "namespace", namespace, "name", virtualServiceName) |
| 140 | + if err := r.Create(ctx, virtualService); err != nil { |
| 141 | + log.Error(err, "unable to create virtual service") |
| 142 | + return err |
| 143 | + } |
| 144 | + justCreated = true |
| 145 | + } else { |
| 146 | + log.Error(err, "error getting virtual service") |
| 147 | + return err |
| 148 | + } |
| 149 | + } |
| 150 | + if !justCreated { |
| 151 | + log.Info("Updating virtual service", "namespace", namespace, "name", virtualServiceName) |
| 152 | + if err := r.Update(ctx, foundVirtualService); err != nil { |
| 153 | + log.Error(err, "unable to update virtual service") |
| 154 | + return err |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
0 commit comments