This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
generated from cruise-automation/oss-template
/
virtual_service.go
94 lines (83 loc) · 3.23 KB
/
virtual_service.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
// Copyright 2021 Cruise LLC
//
// 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
// https://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 resource
import (
"context"
networkingistiov1beta1 "istio.io/api/networking/v1beta1"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
)
type VirtualService struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the behavior of a service.
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
// +optional
Spec networkingistiov1beta1.VirtualService `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
}
// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *VirtualService) DeepCopyInto(out *VirtualService) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
return
}
// DeepCopy is a deepcopy function, copying the receiver, creating a new Service.
func (in *VirtualService) DeepCopy() *VirtualService {
if in == nil {
return nil
}
out := new(VirtualService)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is a deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *VirtualService) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// ServiceResource contains the information needed for processing by a Policy
type VirtualServiceResource struct {
VirtualService VirtualService
ResourceName string
ResourceKind string
}
// GetVirtualServiceResource extracts a VirtualServiceResource from an AdmissionRequest
func GetVirtualServiceResource(ctx context.Context, ar *admissionv1.AdmissionRequest) *VirtualServiceResource {
c := GetResourceCache(ctx)
return c.getOrSet(cacheKeyVirtualService, func() interface{} {
return decodeVirtualServiceResource(ar)
}).(*VirtualServiceResource)
}
func decodeVirtualServiceResource(ar *admissionv1.AdmissionRequest) *VirtualServiceResource {
switch ar.Resource {
case metav1.GroupVersionResource{Group: "networking.istio.io", Version: "v1beta1", Resource: "virtualservices"}, metav1.GroupVersionResource{Group: "networking.istio.io", Version: "v1alpha3", Resource: "virtualservices"}:
vsvc := VirtualService{}
if err := decodeObject(ar.Object.Raw, &vsvc); err != nil {
return nil
}
return &VirtualServiceResource{
VirtualService: vsvc,
ResourceName: GetResourceName(vsvc.ObjectMeta),
ResourceKind: "VirtualService",
}
default:
return nil
}
}