github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argoutil/secret.go (about)

     1  // Copyright 2019 ArgoCD Operator Developers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // 	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package argoutil
    16  
    17  import (
    18  	"fmt"
    19  
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"sigs.k8s.io/controller-runtime/pkg/client"
    23  
    24  	argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
    25  	"github.com/argoproj-labs/argocd-operator/common"
    26  )
    27  
    28  // FetchSecret will retrieve the object with the given Name using the provided client.
    29  // The result will be returned.
    30  func FetchSecret(client client.Client, meta metav1.ObjectMeta, name string) (*corev1.Secret, error) {
    31  	a := &argoproj.ArgoCD{}
    32  	a.ObjectMeta = meta
    33  	secret := NewSecretWithName(a, name)
    34  	return secret, FetchObject(client, meta.Namespace, name, secret)
    35  }
    36  
    37  // NewTLSSecret returns a new TLS Secret based on the given metadata with the provided suffix on the Name.
    38  func NewTLSSecret(cr *argoproj.ArgoCD, suffix string) *corev1.Secret {
    39  	secret := NewSecretWithSuffix(cr, suffix)
    40  	secret.Type = corev1.SecretTypeTLS
    41  	return secret
    42  }
    43  
    44  // NewSecret returns a new Secret based on the given metadata.
    45  func NewSecret(cr *argoproj.ArgoCD) *corev1.Secret {
    46  	return &corev1.Secret{
    47  		ObjectMeta: metav1.ObjectMeta{
    48  			Labels: LabelsForCluster(cr),
    49  		},
    50  		Type: corev1.SecretTypeOpaque,
    51  	}
    52  }
    53  
    54  // NewSecretWithName returns a new Secret based on the given metadata with the provided Name.
    55  func NewSecretWithName(cr *argoproj.ArgoCD, name string) *corev1.Secret {
    56  	secret := NewSecret(cr)
    57  
    58  	secret.ObjectMeta.Name = name
    59  	secret.ObjectMeta.Namespace = cr.Namespace
    60  	secret.ObjectMeta.Labels[common.ArgoCDKeyName] = name
    61  
    62  	return secret
    63  }
    64  
    65  // NewSecretWithSuffix returns a new Secret based on the given metadata with the provided suffix on the Name.
    66  func NewSecretWithSuffix(cr *argoproj.ArgoCD, suffix string) *corev1.Secret {
    67  	return NewSecretWithName(cr, fmt.Sprintf("%s-%s", cr.Name, suffix))
    68  }