Skip to content

Commit

Permalink
Merge pull request #288 from canonical/refactor/k8s-client
Browse files Browse the repository at this point in the history
refactor: k8s client with externalized kubeconfig file path
  • Loading branch information
BarcoMasile authored Apr 23, 2024
2 parents 0403266 + 714dffd commit b8da4b8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This is the Admin UI for the Canonical Identity Platform.
- `LOG_FILE`: file where to dump logs, defaults to `log.txt`
- `PORT `: http server port, defaults to `8080`
- `DEBUG`: debugging flag for hydra and kratos clients
- `KUBECONFIG_FILE`: optional path of kube config file, default to empty string
- `KRATOS_PUBLIC_URL`: Kratos public endpoints address
- `KRATOS_ADMIN_URL`: Kratos admin endpoints address
- `HYDRA_ADMIN_URL`: Hydra admin endpoints address
Expand Down
6 changes: 3 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2024 Canonical Ltd
// SPDX-License-Identifier: AGPL
// Copyright 2024 Canonical Ltd.
// SPDX-License-Identifier: AGPL-3.0

package cmd

Expand Down Expand Up @@ -85,7 +85,7 @@ func serve() {
extCfg.SetAuthorizer(extCfg.OpenFGA())
}

k8sCoreV1, err := k8s.NewCoreV1Client("")
k8sCoreV1, err := k8s.NewCoreV1Client(specs.KubeconfigFile)

if err != nil {
panic(err)
Expand Down
6 changes: 4 additions & 2 deletions internal/config/specs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2024 Canonical Ltd
// SPDX-License-Identifier: AGPL
// Copyright 2024 Canonical Ltd.
// SPDX-License-Identifier: AGPL-3.0

package config

Expand All @@ -16,6 +16,8 @@ type EnvSpec struct {

Debug bool `envconfig:"debug" default:"false"`

KubeconfigFile string `envconfig:"kubeconfig_file"`

KratosPublicURL string `envconfig:"kratos_public_url" required:"true"`
KratosAdminURL string `envconfig:"kratos_admin_url" required:"true"`
HydraAdminURL string `envconfig:"hydra_admin_url" required:"true"`
Expand Down
19 changes: 6 additions & 13 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2024 Canonical Ltd
// SPDX-License-Identifier: AGPL
// Copyright 2024 Canonical Ltd.
// SPDX-License-Identifier: AGPL-3.0

package k8s

Expand All @@ -17,18 +17,11 @@ func NewCoreV1Client(kubeconfig string) (coreV1.CoreV1Interface, error) {
var config *rest.Config
var err error

if kubeconfig != "" {
// use the current context in kubeconfig
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
return nil, err
}
} else {
// creates the in-cluster config
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
// use the current context in kubeconfig
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
return nil, err
}

// creates the clientset
// clientset, err := kubernetes.NewForConfigAndClient(config, httpClient)
clientset, err := kubernetes.NewForConfig(config)
Expand Down

0 comments on commit b8da4b8

Please sign in to comment.