/
types.go
125 lines (106 loc) · 4.98 KB
/
types.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
package snapshot
import (
"encoding/json"
"fmt"
"reflect"
"strings"
amb "github.com/datawire/ambassador/pkg/api/getambassador.io/v2"
"github.com/datawire/ambassador/pkg/kates"
"github.com/datawire/ambassador/pkg/watt"
gw "sigs.k8s.io/gateway-api/apis/v1alpha1"
)
const ApiVersion = "v1"
const ContentTypeJSON = "application/json"
// SecretRef is a secret reference -- basically, a namespace/name pair.
type SecretRef struct {
Namespace string
Name string
}
// The snapshot type represents a complete configuration snapshot as sent to
// diagd.
type Snapshot struct {
// meta information to identify the ambassador
AmbassadorMeta *AmbassadorMetaInfo
// The Kubernetes field contains all the ambassador inputs from kubernetes.
Kubernetes *KubernetesSnapshot
// The Consul field contains endpoint data for any mappings setup to use a
// consul resolver.
Consul *watt.ConsulSnapshot
// The Deltas field contains a list of deltas to indicate what has changed
// since the prior snapshot. This is only computed for the Kubernetes
// portion of the snapshot. Changes in the Consul endpoint data are not
// reflected in this field.
Deltas []*kates.Delta
// The Invalid field contains any kubernetes resources that have failed
// validation.
Invalid []*kates.Unstructured
Raw json.RawMessage `json:"-"`
}
type AmbassadorMetaInfo struct {
ClusterID string `json:"cluster_id"`
AmbassadorID string `json:"ambassador_id"`
AmbassadorVersion string `json:"ambassador_version"`
KubeVersion string `json:"kube_version"`
}
type KubernetesSnapshot struct {
// k8s resources
IngressClasses []*kates.IngressClass `json:"ingressclasses"`
Ingresses []*kates.Ingress `json:"ingresses"`
Services []*kates.Service `json:"service"`
Endpoints []*kates.Endpoints `json:"Endpoints"`
// ambassador resources
Hosts []*amb.Host `json:"Host"`
Mappings []*amb.Mapping `json:"Mapping"`
TCPMappings []*amb.TCPMapping `json:"TCPMapping"`
Modules []*amb.Module `json:"Module"`
TLSContexts []*amb.TLSContext `json:"TLSContext"`
// plugin services
AuthServices []*amb.AuthService `json:"AuthService"`
RateLimitServices []*amb.RateLimitService `json:"RateLimitService"`
LogServices []*amb.LogService `json:"LogService"`
TracingServices []*amb.TracingService `json:"TracingService"`
DevPortals []*amb.DevPortal `json:"DevPortal"`
// resolvers
ConsulResolvers []*amb.ConsulResolver `json:"ConsulResolver"`
KubernetesEndpointResolvers []*amb.KubernetesEndpointResolver `json:"KubernetesEndpointResolver"`
KubernetesServiceResolvers []*amb.KubernetesServiceResolver `json:"KubernetesServiceResolver"`
// gateway api
GatewayClasses []*gw.GatewayClass
Gateways []*gw.Gateway
HTTPRoutes []*gw.HTTPRoute
// It is safe to ignore AmbassadorInstallation, ambassador doesn't need to look at those, just
// the operator.
KNativeClusterIngresses []*kates.Unstructured `json:"clusteringresses.networking.internal.knative.dev,omitempty"`
KNativeIngresses []*kates.Unstructured `json:"ingresses.networking.internal.knative.dev,omitempty"`
K8sSecrets []*kates.Secret `json:"-"` // Secrets from Kubernetes
FSSecrets map[SecretRef]*kates.Secret `json:"-"` // Secrets from the filesystem
Secrets []*kates.Secret `json:"secret"` // Secrets we'll feed to Ambassador
Annotations []kates.Object `json:"-"`
// this is only for the saas app agent com and ambassador agent to communicate about service
// backends. yes, i know this isn't an ambassador input techinically, but putting this here
// makes things _much_ easier when giving the mothership (aka saas app's agent com) a single
// source of the state of the cluster
Pods []*kates.Pod `json:"Pods,omitempty"`
// ArgoRollouts represents the argo-rollout CRD state of the world that may or may not be present
// in the client's cluster. For this reason, Rollouts resources are fetched making use of the
// k8s dynamic client that returns an unstructured.Unstructured object. This is a better strategy
// for Ambassador code base for the following reasons:
// - it is forward compatible
// - no need to maintain types defined by the Argo projects
// - no unnecessary overhead Marshaling/Unmarshaling it into json as the state is opaque to
// Ambassador.
ArgoRollouts []*kates.Unstructured `json:"ArgoRollouts,omitempty"`
// ArgoApplications represents the argo-rollout CRD state of the world that may or may not be present
// in the client's cluster. For reasons why this is defined as unstructured see ArgoRollouts attribute.
ArgoApplications []*kates.Unstructured `json:"ArgoApplications,omitempty"`
}
func (a *KubernetesSnapshot) Render() string {
result := &strings.Builder{}
v := reflect.ValueOf(a)
t := v.Type().Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
result.WriteString(fmt.Sprintf("%s: %d\n", f.Name, reflect.Indirect(v).Field(i).Len()))
}
return result.String()
}