-
Notifications
You must be signed in to change notification settings - Fork 591
/
types.go
58 lines (46 loc) · 1.81 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
/*
Copyright 2018 The Kubernetes Authors.
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 util
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
)
// Endpoint describes a kubernetes endpoint, same as a target in Kong.
type Endpoint struct {
// Address IP address of the endpoint
Address string `json:"address"`
// Port number of the TCP port
Port string `json:"port"`
}
// TypeMeta is stripped after unmarshaling into Go struct due to the issue described in
// https://github.com/kubernetes/kubernetes/issues/3030, but we need it for various purposes.
// This function is adopted from https://github.com/kubernetes/cli-runtime/blob/v0.28.2/pkg/printers/typesetter.go#L39-L72
// It has been modified to remove the io.Writer output and to use the scheme package directly instead of the Typer helper.
// PopulateTypeMeta adds GVK information to a runtime.Object that may not have it available in the object TypeMeta.
func PopulateTypeMeta(obj runtime.Object, s *runtime.Scheme) error {
gvks, _, err := s.ObjectKinds(obj)
if err != nil {
return fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err)
}
for _, gvk := range gvks {
if len(gvk.Kind) == 0 {
continue
}
if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
continue
}
obj.GetObjectKind().SetGroupVersionKind(gvk)
break
}
return nil
}