|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + configuration2 "github.com/nginxinc/kubernetes-nginx-ingress/internal/configuration" |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + "k8s.io/client-go/kubernetes" |
| 10 | + "k8s.io/client-go/rest" |
| 11 | + "k8s.io/client-go/tools/clientcmd" |
| 12 | + "k8s.io/client-go/util/homedir" |
| 13 | + "path/filepath" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + logrus.SetLevel(logrus.DebugLevel) |
| 18 | + err := run() |
| 19 | + if err != nil { |
| 20 | + logrus.Fatal(err) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func run() error { |
| 25 | + logrus.Info("configuration-test-harness::run") |
| 26 | + |
| 27 | + ctx := context.Background() |
| 28 | + var err error |
| 29 | + |
| 30 | + k8sClient, err := buildKubernetesClient() |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf(`error building a Kubernetes client: %w`, err) |
| 33 | + } |
| 34 | + |
| 35 | + configuration, err := configuration2.NewSettings(ctx, k8sClient) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf(`error occurred creating configuration: %w`, err) |
| 38 | + } |
| 39 | + |
| 40 | + err = configuration.Initialize() |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf(`error occurred initializing configuration: %w`, err) |
| 43 | + } |
| 44 | + |
| 45 | + go configuration.Run() |
| 46 | + |
| 47 | + <-ctx.Done() |
| 48 | + |
| 49 | + return err |
| 50 | +} |
| 51 | + |
| 52 | +func buildKubernetesClient() (*kubernetes.Clientset, error) { |
| 53 | + logrus.Debug("Watcher::buildKubernetesClient") |
| 54 | + |
| 55 | + var kubeconfig *string |
| 56 | + var k8sConfig *rest.Config |
| 57 | + |
| 58 | + k8sConfig, err := rest.InClusterConfig() |
| 59 | + if errors.Is(err, rest.ErrNotInCluster) { |
| 60 | + if home := homedir.HomeDir(); home != "" { |
| 61 | + path := filepath.Join(home, ".kube", "config") |
| 62 | + kubeconfig = &path |
| 63 | + |
| 64 | + k8sConfig, err = clientcmd.BuildConfigFromFlags("", *kubeconfig) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf(`error occurred building the kubeconfig: %w`, err) |
| 67 | + } |
| 68 | + } else { |
| 69 | + return nil, fmt.Errorf(`not running in a Cluster: %w`, err) |
| 70 | + } |
| 71 | + } else if err != nil { |
| 72 | + return nil, fmt.Errorf(`error occurred getting the Cluster config: %w`, err) |
| 73 | + } |
| 74 | + |
| 75 | + client, err := kubernetes.NewForConfig(k8sConfig) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf(`error occurred creating a client: %w`, err) |
| 78 | + } |
| 79 | + return client, nil |
| 80 | +} |
0 commit comments