-
Notifications
You must be signed in to change notification settings - Fork 78
/
clients.go
202 lines (177 loc) · 4.91 KB
/
clients.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package clients
import (
"context"
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
"github.com/openshift-pipelines/pipelines-as-code/pkg/generated/clientset/versioned"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/pkg/errors"
versioned2 "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"go.uber.org/zap"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
// most programming languages do not have a timeout, but c# does a default
// of 100 seconds so using that value.
ConnectMaxWaitTime = 100 * time.Second
RequestMaxWaitTime = 100 * time.Second
)
type Clients struct {
ClientInitialized bool
PipelineAsCode versioned.Interface
Tekton versioned2.Interface
Kube kubernetes.Interface
HTTP http.Client
Log *zap.SugaredLogger
Dynamic dynamic.Interface
consoleUIMutex *sync.Mutex
consoleUI consoleui.Interface
}
func (c *Clients) InitClients() {
c.consoleUIMutex = &sync.Mutex{}
}
func (c *Clients) GetURL(ctx context.Context, url string) ([]byte, error) {
nctx, cancel := context.WithTimeout(ctx, RequestMaxWaitTime)
defer cancel()
req, err := http.NewRequestWithContext(nctx, http.MethodGet, url, nil)
if err != nil {
return []byte{}, err
}
res, err := c.HTTP.Do(req)
if err != nil {
return []byte{}, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("Non-OK HTTP status: %d", res.StatusCode)
}
data, err := io.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Set kube client based on config.
func (c *Clients) kubeClient(config *rest.Config) (kubernetes.Interface, error) {
k8scs, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrapf(err, "failed to create k8s client from config")
}
return k8scs, nil
}
func (c *Clients) dynamicClient(config *rest.Config) (dynamic.Interface, error) {
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, errors.Wrapf(err, "failed to create dynamic client from config")
}
return dynamicClient, err
}
func (c *Clients) kubeConfig(info *info.Info) (*rest.Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if info.Kube.ConfigPath != "" {
loadingRules.ExplicitPath = info.Kube.ConfigPath
}
configOverrides := &clientcmd.ConfigOverrides{}
if info.Kube.Context != "" {
configOverrides.CurrentContext = info.Kube.Context
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
if info.Kube.Namespace == "" {
namespace, _, err := kubeConfig.Namespace()
if err != nil {
return nil, errors.Wrap(err, "Couldn't get kubeConfiguration namespace")
}
info.Kube.Namespace = namespace
}
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "Parsing kubeconfig failed")
}
return config, nil
}
func (c *Clients) tektonClient(config *rest.Config) (versioned2.Interface, error) {
cs, err := versioned2.NewForConfig(config)
if err != nil {
return nil, err
}
return cs, nil
}
func (c *Clients) pacClient(config *rest.Config) (versioned.Interface, error) {
cs, err := versioned.NewForConfig(config)
if err != nil {
return nil, err
}
return cs, nil
}
func (c *Clients) consoleUIClient(ctx context.Context, dynamic dynamic.Interface, info *info.Info) consoleui.Interface {
return consoleui.New(ctx, dynamic, info)
}
func (c *Clients) NewClients(ctx context.Context, info *info.Info) error {
if c.consoleUIMutex == nil {
c.consoleUIMutex = &sync.Mutex{}
}
if c.ClientInitialized {
return nil
}
prod, _ := zap.NewProduction()
logger := prod.Sugar()
defer func() {
_ = logger.Sync() // flushes buffer, if any
}()
c.Log = logger
c.HTTP = http.Client{
Timeout: RequestMaxWaitTime,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: ConnectMaxWaitTime,
}).DialContext,
},
}
config, err := c.kubeConfig(info)
if err != nil {
return err
}
config.QPS = 50
config.Burst = 50
c.Kube, err = c.kubeClient(config)
if err != nil {
return err
}
c.Tekton, err = c.tektonClient(config)
if err != nil {
return err
}
c.PipelineAsCode, err = c.pacClient(config)
if err != nil {
return err
}
c.Dynamic, err = c.dynamicClient(config)
if err != nil {
return err
}
c.SetConsoleUI(c.consoleUIClient(ctx, c.Dynamic, info))
c.ClientInitialized = true
return nil
}
func (c *Clients) ConsoleUI() consoleui.Interface {
c.consoleUIMutex.Lock()
defer c.consoleUIMutex.Unlock()
return c.consoleUI
}
func (c *Clients) SetConsoleUI(consoleUI consoleui.Interface) {
if c.consoleUIMutex == nil {
c.consoleUIMutex = &sync.Mutex{}
}
c.consoleUIMutex.Lock()
defer c.consoleUIMutex.Unlock()
c.consoleUI = consoleUI
}