Skip to content

Commit

Permalink
test: (SVPI-632, SVPI-633) add remote secrets environments tests (kon…
Browse files Browse the repository at this point in the history
…flux-ci#815)

* add remote secrets environments tests

* add more checks

* fix

* remove env and use existing method
  • Loading branch information
albarbaro authored Oct 13, 2023
1 parent f42c52e commit bf95025
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ github.com/devfile/api/v2 v2.2.0/go.mod h1:dN7xFrOVG+iPqn4UKGibXLd5oVsdE8XyK9OEb
github.com/devfile/api/v2 v2.2.1-alpha.0.20230413012049-a6c32fca0dbd h1:HpGR728CfB6BB9ZuFtQb0UeTIYNFgpuGsuoMOJNMUTM=
github.com/devfile/api/v2 v2.2.1-alpha.0.20230413012049-a6c32fca0dbd/go.mod h1:qp8jcw12y1JdCsxjK/7LJ7uWaJOxcY1s2LUk5PhbkbM=
github.com/devfile/library v1.2.1-0.20211104222135-49d635cb492f/go.mod h1:uFZZdTuRqA68FVe/JoJHP92CgINyQkyWnM2Qyiim+50=
github.com/devfile/library v1.2.1-0.20220308191614-f0f7e11b17de h1:jImHtiAxjyul1UkPmf6C3EMS5wqNz+k84LKkCXkeqws=
github.com/devfile/library v1.2.1-0.20220308191614-f0f7e11b17de/go.mod h1:GSPfJaBg0+bBjBHbwBE5aerJLH6tWGQu2q2rHYd9czM=
github.com/devfile/library/v2 v2.0.1/go.mod h1:paJ0PARAVy0br13VpBEQ4fO3rZVDxWtooQ29+23PNBk=
github.com/devfile/library/v2 v2.2.1-0.20230418160146-e75481b7eebd h1:YHSwUdfWsG9Qk7Vn+NfafELv6+G6a43RRE/NjS0TfK0=
Expand Down
49 changes: 49 additions & 0 deletions pkg/utils/remotesecret/remote_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

rs "github.com/redhat-appstudio/remote-secret/api/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -31,6 +32,30 @@ func (s *RemoteSecretController) CreateRemoteSecret(name, namespace string, targ
return &remoteSecret, nil
}

// CreateRemoteSecretWithLabels creates a RemoteSecret object with specified labels
func (s *RemoteSecretController) CreateRemoteSecretWithLabels(name, namespace string, targetSecretName string, labels map[string]string) (*rs.RemoteSecret, error) {
remoteSecret := rs.RemoteSecret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: rs.RemoteSecretSpec{
Secret: rs.LinkableSecretSpec{
Name: targetSecretName,
},
},
}

remoteSecret.ObjectMeta.Labels = labels

err := s.KubeRest().Create(context.TODO(), &remoteSecret)
if err != nil {
return nil, err
}
return &remoteSecret, nil
}

// GetRemoteSecret returns the requested RemoteSecret object
func (s *RemoteSecretController) GetRemoteSecret(name, namespace string) (*rs.RemoteSecret, error) {
namespacedName := types.NamespacedName{
Expand Down Expand Up @@ -59,3 +84,27 @@ func (s *RemoteSecretController) GetTargetSecretName(targets []rs.TargetStatus,

return targetSecretName
}

// CreateUploadSecret creates an Upload secret object to inject data in a Remote Secret
func (s *RemoteSecretController) CreateUploadSecret(name, namespace string, remoteSecretName string, stringData map[string]string) (*corev1.Secret, error) {
uploadSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
rs.UploadSecretLabel: "remotesecret",
},
Annotations: map[string]string{
rs.RemoteSecretNameAnnotation: remoteSecretName,
},
},
Type: corev1.SecretTypeOpaque,
StringData: stringData,
}

err := s.KubeRest().Create(context.TODO(), &uploadSecret)
if err != nil {
return nil, err
}
return &uploadSecret, nil
}
36 changes: 36 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"knative.dev/pkg/apis"

devfilePkg "github.com/devfile/library/v2/pkg/devfile"
Expand All @@ -42,6 +44,8 @@ import (
"sigs.k8s.io/yaml"

crclient "sigs.k8s.io/controller-runtime/pkg/client"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

type FailedPipelineRunDetails struct {
Expand Down Expand Up @@ -417,3 +421,35 @@ func GetGithubAppID() (int64, error) {
}
return id, nil
}

// Build a kubeconfig string from an existing client config
func CreateKubeconfigFileForRestConfig(restConfig rest.Config) ([]byte, error) {
clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Server: restConfig.Host,
CertificateAuthorityData: restConfig.CAData,
InsecureSkipTLSVerify: true,
}
contexts := make(map[string]*clientcmdapi.Context)
contexts["default-context"] = &clientcmdapi.Context{
Cluster: "default-cluster",
AuthInfo: "default-user",
}
authinfos := make(map[string]*clientcmdapi.AuthInfo)
authinfos["default-user"] = &clientcmdapi.AuthInfo{
Token: string(restConfig.BearerToken),
}
clientConfig := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: clusters,
Contexts: contexts,
CurrentContext: "default-context",
AuthInfos: authinfos,
}
kubeconfiString, err := clientcmd.Write(clientConfig)
if err != nil {
return []byte{}, nil
}
return kubeconfiString, nil
}
Loading

0 comments on commit bf95025

Please sign in to comment.