-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (93 loc) · 2.72 KB
/
main.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
// Copyright 2023 Undistro 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 main
import (
"context"
"fmt"
"log"
"os"
"path"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/undistro/kubexns/options"
)
func main() {
opts := options.NewFromEnv()
config, err := opts.ToRESTConfig()
if err != nil {
log.Fatalln(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalln(err)
}
client := clientset.CoreV1()
ctx := context.TODO()
var configMaps []corev1.ConfigMap
var secrets []corev1.Secret
for _, nn := range opts.ConfigMaps {
cm, err := client.ConfigMaps(nn.Namespace).Get(ctx, nn.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) && opts.IgnoreNotFound {
continue
}
log.Fatalln("failed to get ConfigMap:", err)
}
configMaps = append(configMaps, *cm)
}
for _, nn := range opts.Secrets {
sec, err := client.Secrets(nn.Namespace).Get(ctx, nn.Name, metav1.GetOptions{})
if errors.IsNotFound(err) && opts.IgnoreNotFound {
continue
}
if err != nil {
log.Fatalln("failed to get Secret:", err)
}
secrets = append(secrets, *sec)
}
if opts.ConfigMapsSelector != "" {
list, err := client.ConfigMaps("").List(ctx, metav1.ListOptions{LabelSelector: opts.ConfigMapsSelector})
if err != nil {
log.Fatalln("failed to list ConfigMaps:", err)
}
configMaps = append(configMaps, list.Items...)
}
if opts.SecretsSelector != "" {
list, err := client.Secrets("").List(ctx, metav1.ListOptions{LabelSelector: opts.SecretsSelector})
if err != nil {
log.Fatalln("failed to list Secrets:", err)
}
secrets = append(secrets, list.Items...)
}
for _, cm := range configMaps {
for k, v := range cm.Data {
name := path.Join(opts.Dir, k)
if err := os.WriteFile(name, []byte(v), opts.DefaultMode); err != nil {
log.Fatalln("failed to write file:", err)
}
fmt.Println(name)
}
}
for _, sec := range secrets {
for k, v := range sec.Data {
name := path.Join(opts.Dir, k)
if err := os.WriteFile(name, v, opts.DefaultMode); err != nil {
log.Fatalln("failed to write file:", err)
}
fmt.Println(name)
}
}
}