Skip to content

Commit

Permalink
Merge pull request #111 from onepanelio/feat/namespace.cleanup
Browse files Browse the repository at this point in the history
feat: search more than just the onepanel namespace
  • Loading branch information
Vafilor authored Oct 26, 2021
2 parents 055bd4e + b6f41e3 commit 2915ef7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cloud/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// by the CLI. CLI will marshal this struct into the correct
// YAML structure for k8s configmap / secret.
type ArtifactRepositoryS3Provider struct {
Source string
KeyFormat string `yaml:"keyFormat"`
Bucket string
Endpoint string
Expand All @@ -36,6 +37,7 @@ type ArtifactRepositoryS3Provider struct {
// by the CLI. CLI will marshal this struct into the correct
// YAML structure for k8s configmap / secret.
type ArtifactRepositoryGCSProvider struct {
Source string
KeyFormat string `yaml:"keyFormat"`
Bucket string
Endpoint string
Expand Down Expand Up @@ -217,6 +219,7 @@ func (a *ArtifactRepositoryS3Provider) MarshalToYaml() (string, error) {
defer encoder.Close()
err := encoder.Encode(&ArtifactRepositoryProvider{
S3: &ArtifactRepositoryS3Provider{
Source: a.Source,
KeyFormat: a.KeyFormat,
Bucket: a.Bucket,
Endpoint: a.Endpoint,
Expand Down Expand Up @@ -251,6 +254,7 @@ func (g *ArtifactRepositoryGCSProvider) MarshalToYaml() (string, error) {
defer encoder.Close()
err := encoder.Encode(&ArtifactRepositoryProvider{
GCS: &ArtifactRepositoryGCSProvider{
Source: g.Source,
KeyFormat: g.KeyFormat,
Bucket: g.Bucket,
Endpoint: g.Endpoint,
Expand Down
3 changes: 3 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func GenerateKustomizeResult(kustomizeTemplate template.Kustomize, options *Gene
return "", err
}
if artifactRepositoryConfig.S3 != nil {
artifactRepositoryConfig.S3.Source = "s3"
artifactRepositoryConfig.S3.AccessKeySecret.Key = "artifactRepositoryS3AccessKey"
artifactRepositoryConfig.S3.AccessKeySecret.Name = "$(artifactRepositoryS3AccessKeySecretName)"
artifactRepositoryConfig.S3.SecretKeySecret.Key = "artifactRepositoryS3SecretKey"
Expand All @@ -314,6 +315,7 @@ func GenerateKustomizeResult(kustomizeTemplate template.Kustomize, options *Gene
}

artifactRepositoryConfig.S3 = &storage.ArtifactRepositoryS3Provider{
Source: "gcs",
KeyFormat: artifactRepositoryConfig.GCS.KeyFormat,
Bucket: artifactRepositoryConfig.GCS.Bucket,
Endpoint: fmt.Sprintf("minio-gateway.%v.svc.cluster.local:9000", defaultNamespace),
Expand Down Expand Up @@ -345,6 +347,7 @@ func GenerateKustomizeResult(kustomizeTemplate template.Kustomize, options *Gene
yamlFile.Put("artifactRepositoryServiceAccountKey", base64.StdEncoding.EncodeToString([]byte(artifactRepositoryConfig.GCS.ServiceAccountKey)))
} else if artifactRepositoryConfig.ABS != nil {
artifactRepositoryConfig.S3 = &storage.ArtifactRepositoryS3Provider{
Source: "abs",
KeyFormat: artifactRepositoryConfig.ABS.KeyFormat,
Bucket: artifactRepositoryConfig.ABS.Container,
Endpoint: fmt.Sprintf("minio-gateway.%v.svc.cluster.local:9000", defaultNamespace),
Expand Down
46 changes: 38 additions & 8 deletions util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"context"
"fmt"
"github.com/pkg/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -17,6 +18,24 @@ import (

type Config = restclient.Config

const onepanelEnabledLabelKey = "onepanel.io/enabled"

func ListOnepanelEnabledNamespaces(c *kubernetes.Clientset) (namespaces []string, err error) {
namespaceList, err := c.CoreV1().Namespaces().List(context.Background(), v1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", onepanelEnabledLabelKey, "true"),
})

if err != nil {
return
}

for _, ns := range namespaceList.Items {
namespaces = append(namespaces, ns.Name)
}

return
}

// NewConfig creates a new, default, configuration for kubernetes
func NewConfig() (config *Config, err error) {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
Expand Down Expand Up @@ -44,18 +63,29 @@ func GetBearerToken(in *restclient.Config, explicitKubeConfigPath string, servic
if err != nil {
return "", serviceAccountName, errors.Errorf("Could not get kubeClient")
}
ns := "onepanel"
secrets, err := kubeClient.CoreV1().Secrets(ns).List(context.Background(), v1.ListOptions{})

namespaces := []string{"onepanel"}
moreNamespaces, err := ListOnepanelEnabledNamespaces(kubeClient)
if err != nil {
return "", serviceAccountName, errors.Errorf("Could not get %s secrets.", ns)
return "", "", err
}
search := `^` + serviceAccountName + `-token-`
re := regexp.MustCompile(search)
for _, secret := range secrets.Items {
if re.Find([]byte(secret.ObjectMeta.Name)) != nil {
return string(secret.Data["token"]), serviceAccountName, nil
namespaces = append(namespaces, moreNamespaces...)

for _, namespace := range namespaces {
secrets, err := kubeClient.CoreV1().Secrets(namespace).List(context.Background(), v1.ListOptions{})
if err != nil {
return "", serviceAccountName, errors.Errorf("Could not get %s secrets.", namespace)
}

search := `^` + serviceAccountName + `-token-`
re := regexp.MustCompile(search)
for _, secret := range secrets.Items {
if re.Find([]byte(secret.ObjectMeta.Name)) != nil {
return string(secret.Data["token"]), serviceAccountName, nil
}
}
}

return "", serviceAccountName, errors.Errorf("could not find a token")
}

Expand Down

0 comments on commit 2915ef7

Please sign in to comment.