/
istio.go
144 lines (125 loc) · 4.97 KB
/
istio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2022 Avesha, Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package serviceimport
import (
"context"
"fmt"
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
"github.com/kubeslice/worker-operator/controllers"
"github.com/kubeslice/worker-operator/pkg/logger"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)
// reconcile istio resources like virtualService and serviceentry to facilitate service to service connectivity
func (r *Reconciler) reconcileIstio(ctx context.Context, serviceimport *kubeslicev1beta1.ServiceImport) (ctrl.Result, error, bool) {
log := logger.FromContext(ctx).WithValues("type", "Istio")
debugLog := log.V(1)
slice, err := controllers.GetSlice(ctx, r.Client, serviceimport.Spec.Slice)
if err != nil {
log.Error(err, "Unable to fetch slice for serviceexport")
return ctrl.Result{}, err, true
}
if slice.Status.SliceConfig == nil {
err := fmt.Errorf("sliceconfig is not reconciled from hub")
log.Error(err, "unable to reconcile istio")
return ctrl.Result{}, err, true
}
if slice.Status.SliceConfig.ExternalGatewayConfig == nil ||
slice.Status.SliceConfig.ExternalGatewayConfig.GatewayType != "istio" {
debugLog.Info("istio not enabled for slice, skipping reconcilation")
return ctrl.Result{}, nil, false
}
debugLog.Info("reconciling istio")
// Create k8s service for app pods to connect
res, err, requeue := r.ReconcileService(ctx, serviceimport)
if requeue {
return res, err, requeue
}
egressGatewayConfig := slice.Status.SliceConfig.ExternalGatewayConfig.Egress
// Add service entries and virtualServices for app pods in the slice to be able to route traffic through slice.
// Endpoints are load balanced at equal weights.
var serviceReconcilationNamespace string
if egressGatewayConfig != nil && egressGatewayConfig.Enabled {
serviceReconcilationNamespace = controllers.ControlPlaneNamespace
} else {
serviceReconcilationNamespace = serviceimport.Namespace
}
res, err, requeue = r.ReconcileServiceEntries(ctx, serviceimport, serviceReconcilationNamespace)
if requeue {
return res, err, requeue
}
if serviceReconcilationNamespace == controllers.ControlPlaneNamespace {
res, err, requeue = r.ReconcileVirtualServiceEgress(ctx, serviceimport)
if requeue {
return res, err, requeue
}
} else {
res, err, requeue = r.ReconcileVirtualServiceNonEgress(ctx, serviceimport)
if requeue {
return res, err, requeue
}
}
return ctrl.Result{}, nil, false
}
func (r *Reconciler) ReconcileService(ctx context.Context, serviceimport *kubeslicev1beta1.ServiceImport) (ctrl.Result, error, bool) {
log := logger.FromContext(ctx).WithValues("type", "Istio Service")
svc := &corev1.Service{}
err := r.Get(ctx, types.NamespacedName{
Name: serviceimport.Name,
Namespace: serviceimport.Namespace,
}, svc)
if err != nil {
if errors.IsNotFound(err) {
// Define a new service
svc := r.serviceForServiceImport(serviceimport)
log.Info("Creating a new Service", "Namespace", svc.Namespace, "Name", svc.Name)
err = r.Create(ctx, svc)
if err != nil {
log.Error(err, "Failed to create new Service", "Namespace", svc.Namespace, "Name", svc.Name)
return ctrl.Result{}, err, true
}
return ctrl.Result{Requeue: true}, nil, true
}
log.Error(err, "Failed to get Service")
return ctrl.Result{}, err, true
}
// TODO handle change of ports
return ctrl.Result{}, nil, false
}
func (r *Reconciler) DeleteIstioResources(ctx context.Context, serviceimport *kubeslicev1beta1.ServiceImport, slice *kubeslicev1beta1.Slice) error {
// We should only clean up resources that were created in the control plane namespace. Setting the service import object
// in the app namespace as the owner reference does not clean up resources in other namespaces.
// Resources in application namespaces are garbage collected because the owner reference for them is set to be the
// service import object, so we do not have to delete them explicitly here.
if slice.Status.SliceConfig.ExternalGatewayConfig == nil ||
slice.Status.SliceConfig.ExternalGatewayConfig.Egress == nil ||
!slice.Status.SliceConfig.ExternalGatewayConfig.Egress.Enabled {
return nil
}
err := r.DeleteIstioServiceEntries(ctx, serviceimport)
if err != nil {
return err
}
err = r.DeleteIstioVirtualServicesEgress(ctx, serviceimport)
if err != nil {
return err
}
return nil
}