-
Notifications
You must be signed in to change notification settings - Fork 173
/
models.go
181 lines (145 loc) · 4.78 KB
/
models.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package kubectl
import (
"encoding/json"
"fmt"
)
type ResourceType string
const (
ResourceTypeDeployment ResourceType = "deployment"
ResourceTypeIngress ResourceType = "ing"
ResourceTypeService ResourceType = "svc"
KubeConfigEnvVarName string = "KUBECONFIG"
)
type Resource struct {
ApiVersion string `json:"apiVersion" yaml:"apiVersion"`
Kind string `json:"kind" yaml:"kind"`
Metadata ResourceMetadata `json:"metadata" yaml:"metadata"`
}
type List[T any] struct {
Resource
Items []T `json:"items" yaml:"items"`
}
type ResourceWithSpec[T any, S any] struct {
Resource
Spec T `json:"spec" yaml:"spec"`
Status S `json:"status" yaml:"status"`
}
type ResourceMetadata struct {
Name string `json:"name" yaml:"name"`
Namespace string `json:"namespace" yaml:"namespace"`
Annotations map[string]any
}
type Deployment ResourceWithSpec[DeploymentSpec, DeploymentStatus]
type DeploymentSpec struct {
Replicas int `json:"replicas" yaml:"replicas"`
}
type DeploymentStatus struct {
AvailableReplicas int `json:"availableReplicas" yaml:"availableReplicas"`
ReadyReplicas int `json:"readyReplicas" yaml:"readyReplicas"`
Replicas int `json:"replicas" yaml:"replicas"`
UpdatedReplicas int `json:"updatedReplicas" yaml:"updatedReplicas"`
}
type Ingress ResourceWithSpec[IngressSpec, IngressStatus]
type IngressSpec struct {
IngressClassName string `json:"ingressClassName" yaml:"ingressClassName"`
Tls []IngressTls `json:"tls" yaml:"tls"`
Rules []IngressRule `json:"rules" yaml:"rules"`
}
type IngressTls struct {
Hosts []string `json:"hosts" yaml:"hosts"`
SecretName string `json:"secretName" yaml:"secretName"`
}
type IngressRule struct {
Host *string `json:"host" yaml:"host"`
Http IngressRuleHttp `json:"http" yaml:"http"`
}
type IngressRuleHttp struct {
Paths []IngressPath `json:"paths" yaml:"paths"`
}
type IngressPath struct {
Path string `json:"path" yaml:"path"`
PathType string `json:"pathType" yaml:"pathType"`
}
type IngressStatus struct {
LoadBalancer LoadBalancer `json:"loadBalancer" yaml:"loadBalancer"`
}
type LoadBalancer struct {
Ingress []LoadBalancerIngress `json:"ingress" yaml:"ingress"`
}
type LoadBalancerIngress struct {
Ip string `json:"ip" yaml:"ip"`
}
type Service ResourceWithSpec[ServiceSpec, ServiceStatus]
type ServiceType string
const (
ServiceTypeClusterIp ServiceType = "ClusterIP"
ServiceTypeLoadBalancer ServiceType = "LoadBalancer"
ServiceTypeNodePort ServiceType = "NodePort"
ServiceTypeExternalName ServiceType = "ExternalName"
)
type ServiceSpec struct {
Type ServiceType `json:"type" yaml:"type"`
ClusterIp string `json:"clusterIP" yaml:"clusterIP"`
ClusterIps []string `json:"clusterIPs" yaml:"clusterIPs"`
Ports []Port `json:"ports" yaml:"ports"`
}
type ServiceStatus struct {
LoadBalancer LoadBalancer `json:"loadBalancer" yaml:"loadBalancer"`
}
type Port struct {
Port int `json:"port"`
// The target port can be a valid port number or well known service name like 'redis'
TargetPort any `json:"targetPort" yaml:"targetPort"`
Protocol string `json:"protocol" yaml:"protocol"`
}
func (p *Port) UnmarshalJSON(data []byte) error {
var aux struct {
Port int `json:"port" yaml:"port"`
TargetPort any `json:"targetPort" yaml:"targetPort"`
Protocol string `json:"protocol" yaml:"protocol"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
p.Port = aux.Port
p.Protocol = aux.Protocol
switch v := aux.TargetPort.(type) {
case string, int, float64:
p.TargetPort = v
return nil
default:
return fmt.Errorf("unsupported type for TargetPort")
}
}
type KubeConfig struct {
ApiVersion string `yaml:"apiVersion"`
Clusters []*KubeCluster `yaml:"clusters"`
Contexts []*KubeContext `yaml:"contexts"`
Users []*KubeUser `yaml:"users"`
Kind string `yaml:"kind"`
CurrentContext string `yaml:"current-context"`
Preferences KubePreferences `yaml:"preferences"`
}
type KubeCluster struct {
Name string `yaml:"name"`
Cluster KubeClusterData `yaml:"cluster"`
}
type KubeClusterData struct {
CertificateAuthorityData string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
}
type KubeContext struct {
Name string `yaml:"name"`
Context KubeContextData `yaml:"context"`
}
type KubeContextData struct {
Cluster string `yaml:"cluster"`
Namespace string `yaml:"namespace"`
User string `yaml:"user"`
}
type KubeUser struct {
Name string `yaml:"name"`
KubeUserData KubeUserData `yaml:"user"`
}
type KubeUserData map[string]any
type KubePreferences map[string]any