Introduce options type to build client.
parent
e4ff30addb
commit
4f9e929f5d
|
@ -17,8 +17,6 @@ limitations under the License.
|
|||
package basic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/digitalocean/clusterlint/checks"
|
||||
"github.com/digitalocean/clusterlint/kube"
|
||||
)
|
||||
|
@ -50,7 +48,6 @@ func (b *barePodCheck) Run(objects *kube.Objects) ([]checks.Diagnostic, error) {
|
|||
var diagnostics []checks.Diagnostic
|
||||
for _, pod := range objects.Pods.Items {
|
||||
pod := pod
|
||||
fmt.Println(pod.ObjectMeta.OwnerReferences)
|
||||
if len(pod.ObjectMeta.OwnerReferences) == 0 {
|
||||
d := checks.Diagnostic{
|
||||
Check: b.Name(),
|
||||
|
|
|
@ -45,6 +45,10 @@ func main() {
|
|||
Name: "context",
|
||||
Usage: "context for the kubernetes client. default: current context",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "timeout",
|
||||
Usage: "configure timeout for the kubernetes client. default: 30s",
|
||||
},
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
|
@ -126,7 +130,7 @@ func listChecks(c *cli.Context) error {
|
|||
|
||||
// runChecks runs all the checks based on the flags passed.
|
||||
func runChecks(c *cli.Context) error {
|
||||
client, err := kube.NewClient(c.GlobalString("kubeconfig"), c.GlobalString("context"))
|
||||
client, err := kube.NewClient(kube.WithConfigFile(c.GlobalString("kubeconfig")), kube.WithKubeContext(c.GlobalString("context")), kube.WithTimeout(c.GlobalString("timeout")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ limitations under the License.
|
|||
package kube
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
ar "k8s.io/api/admissionregistration/v1beta1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
@ -135,46 +137,40 @@ func (c *Client) FetchObjects() (*Objects, error) {
|
|||
}
|
||||
|
||||
// NewClient builds a kubernetes client to interact with the live cluster.
|
||||
// The kube config file path and the context must be specified for the client
|
||||
// The kube config file path or the kubeconfig yaml must be specified
|
||||
// If not specified, defaults are assumed - configPath: ~/.kube/config, configContext: current context
|
||||
func NewClient(configPath, configContext string) (*Client, error) {
|
||||
func NewClient(opts ...Option) (*Client, error) {
|
||||
timeout, _ := time.ParseDuration("30s")
|
||||
defOpts := &options{
|
||||
timeout: timeout,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if err := opt(defOpts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var config *rest.Config
|
||||
var err error
|
||||
|
||||
if configContext != "" {
|
||||
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: configPath},
|
||||
&clientcmd.ConfigOverrides{
|
||||
CurrentContext: configContext,
|
||||
}).ClientConfig()
|
||||
} else {
|
||||
config, err = clientcmd.BuildConfigFromFlags("", configPath)
|
||||
if defOpts.path != "" {
|
||||
if defOpts.kubeContext != "" {
|
||||
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: defOpts.path},
|
||||
&clientcmd.ConfigOverrides{
|
||||
CurrentContext: defOpts.kubeContext,
|
||||
}).ClientConfig()
|
||||
} else if defOpts.yaml != nil {
|
||||
config, err = clientcmd.RESTConfigFromKubeConfig(defOpts.yaml)
|
||||
} else {
|
||||
config, err = clientcmd.BuildConfigFromFlags("", defOpts.path)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
KubeClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// BuildClient builds a kubernetes client from the yaml to interact with the live cluster.
|
||||
func BuildClient(yaml []byte) (*Client, error) {
|
||||
var config *rest.Config
|
||||
var err error
|
||||
|
||||
config, err = clientcmd.RESTConfigFromKubeConfig(yaml)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.Timeout = defOpts.timeout
|
||||
client, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Copyright 2019 DigitalOcean
|
||||
|
||||
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 kube
|
||||
|
||||
import "time"
|
||||
|
||||
type options struct {
|
||||
path string
|
||||
kubeContext string
|
||||
yaml []byte
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// Option function that allows injecting options while building kube.Client.
|
||||
type Option func(*options) error
|
||||
|
||||
// WithConfigFile returns an Option injected with a config file path.
|
||||
func WithConfigFile(path string) Option {
|
||||
return func(o *options) error {
|
||||
o.path = path
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithKubeContext returns an Option injected with a kubernetes context.
|
||||
func WithKubeContext(kubeContext string) Option {
|
||||
return func(o *options) error {
|
||||
o.kubeContext = kubeContext
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithYaml returns an Option injected with a kubeconfig yaml.
|
||||
func WithYaml(yaml []byte) Option {
|
||||
return func(o *options) error {
|
||||
o.yaml = yaml
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithTimeout returns an Option injected with a timeout option while building client.
|
||||
// A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix,
|
||||
// such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||
func WithTimeout(t string) Option {
|
||||
return func(o *options) error {
|
||||
if t != "" {
|
||||
timeout, err := time.ParseDuration(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.timeout = timeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue