forked from nocalhost/nocalhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
133 lines (119 loc) · 3.74 KB
/
utils.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
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package k8sutils
import (
"context"
"github.com/lsutils/nocalhost/internal/nhctl/fp"
"github.com/lsutils/nocalhost/internal/nhctl/nocalhost_path"
"github.com/lsutils/nocalhost/internal/nhctl/utils"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
runtimejson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/cache"
toolswatch "k8s.io/client-go/tools/watch"
"k8s.io/kubectl/pkg/scheme"
"time"
)
func ValidateDNS1123Name(name string) bool {
errs := validation.IsDNS1123Subdomain(name)
if len(errs) == 0 {
return true
} else {
return false
}
}
func WaitPod(client *kubernetes.Clientset, namespace string, listOptions v1.ListOptions,
checker func(*corev1.Pod) bool, timeout time.Duration) error {
return WaitResource(
client,
client.CoreV1().RESTClient(),
namespace,
corev1.SchemeGroupVersion.WithKind("Pod"),
listOptions,
func(i interface{}) bool { return checker(i.(*corev1.Pod)) },
timeout,
)
}
func WaitResource(client *kubernetes.Clientset, g cache.Getter, namespace string, gvk schema.GroupVersionKind,
listOptions v1.ListOptions, checker func(interface{}) bool, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
groupResources, _ := restmapper.GetAPIGroupResources(client)
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)
restMapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return err
}
watchlist := cache.NewFilteredListWatchFromClient(
g,
restMapping.Resource.Resource,
namespace,
func(options *v1.ListOptions) {
options.LabelSelector = listOptions.LabelSelector
options.FieldSelector = listOptions.FieldSelector
})
preConditionFunc := func(store cache.Store) (bool, error) {
if len(store.List()) == 0 {
return false, nil
}
for _, p := range store.List() {
if !checker(p) {
return false, nil
}
}
return true, nil
}
object, err := scheme.Scheme.New(gvk)
if err != nil {
return err
}
conditionFunc := func(e watch.Event) (bool, error) { return checker(e.Object), nil }
event, err := toolswatch.UntilWithSync(ctx, watchlist, object, preConditionFunc, conditionFunc)
if err != nil {
log.Infof("wait resource: %s to ready failed, error: %v, event: %v",
restMapping.Resource.Resource, err, event)
return err
}
return nil
}
// gen or get kubeconfig from local path by kubeconfig content
// we would gen or get kubeconfig file named by hash
func GetOrGenKubeConfigPath(kubeconfigContent string) string {
dir := nocalhost_path.GetNhctlKubeconfigDir(utils.Sha1ToString(kubeconfigContent))
path := fp.NewFilePath(dir)
if path.ReadFile() != "" {
return dir
} else {
_ = path.RelOrAbs("../").Mkdir()
_ = path.WriteFile(kubeconfigContent)
return dir
}
}
func GetObjectMetaData(obj interface{}) *metadataOnlyObject {
var caseSensitiveJsonIterator = runtimejson.CaseSensitiveJSONIterator()
marshal, errs := caseSensitiveJsonIterator.Marshal(obj)
if errs != nil {
return nil
}
v := &metadataOnlyObject{}
if errs = caseSensitiveJsonIterator.Unmarshal(marshal, v); errs != nil {
return nil
}
return v
}
func GetNamespaceAndNameFromObjectMeta(obj interface{}) (namespace, name string, errs error) {
v := GetObjectMetaData(obj)
return v.Namespace, v.Name, nil
}
type metadataOnlyObject struct {
v1.TypeMeta `json:",inline"`
v1.ObjectMeta `json:"metadata,omitempty"`
}