Skip to content

Commit

Permalink
rebase against master
Browse files Browse the repository at this point in the history
Signed-off-by: Jaideep Rao <[email protected]>
  • Loading branch information
jaideepr97 committed Jan 14, 2024
2 parents aa6a9b1 + acee651 commit 6a6847a
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 3 deletions.
1 change: 1 addition & 0 deletions controllers/argocd/policyrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func policyRuleForServer() []v1.PolicyRule {
},
Resources: []string{
"applications",
"applicationsets",
"appprojects",
},
Verbs: []string{
Expand Down
124 changes: 124 additions & 0 deletions controllers/argoutil/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2019 ArgoCD Operator Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package argoutil

import (
"context"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

argoprojv1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
)

// AppendStringMap will append the map `add` to the given map `src` and return the result.
func AppendStringMap(src map[string]string, add map[string]string) map[string]string {
res := src
if len(src) <= 0 {
res = make(map[string]string, len(add))
}
for key, val := range add {
res[key] = val
}
return res
}

// CombineImageTag will return the combined image and tag in the proper format for tags and digests.
func CombineImageTag(img string, tag string) string {
if strings.Contains(tag, ":") {
return fmt.Sprintf("%s@%s", img, tag) // Digest
} else if len(tag) > 0 {
return fmt.Sprintf("%s:%s", img, tag) // Tag
}
return img // No tag, use default
}

// CreateEvent will create a new Kubernetes Event with the given action, message, reason and involved uid.
func CreateEvent(client client.Client, eventType, action, message, reason string, objectMeta metav1.ObjectMeta, typeMeta metav1.TypeMeta) error {
event := newEvent(objectMeta)
event.Action = action
event.Type = eventType
event.InvolvedObject = corev1.ObjectReference{
Name: objectMeta.Name,
Namespace: objectMeta.Namespace,
UID: objectMeta.UID,
ResourceVersion: objectMeta.ResourceVersion,
Kind: typeMeta.Kind,
APIVersion: typeMeta.APIVersion,
}
event.Message = message
event.Reason = reason
event.CreationTimestamp = metav1.Now()
event.FirstTimestamp = event.CreationTimestamp
event.LastTimestamp = event.CreationTimestamp
return client.Create(context.TODO(), event)
}

// FetchObject will retrieve the object with the given namespace and name using the Kubernetes API.
// The result will be stored in the given object.
func FetchObject(client client.Client, namespace string, name string, obj client.Object) error {
return client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, obj)
}

// FetchStorageSecretName will return the name of the Secret to use for the export process.
func FetchStorageSecretName(export *argoprojv1alpha1.ArgoCDExport) string {
name := NameWithSuffix(export.ObjectMeta, "export")
if export.Spec.Storage != nil && len(export.Spec.Storage.SecretName) > 0 {
name = export.Spec.Storage.SecretName
}
return name
}

// IsObjectFound will perform a basic check that the given object exists via the Kubernetes API.
// If an error occurs as part of the check, the function will return false.
func IsObjectFound(client client.Client, namespace string, name string, obj client.Object) bool {
return !apierrors.IsNotFound(FetchObject(client, namespace, name, obj))
}

// NameWithSuffix will return a string using the Name from the given ObjectMeta with the provded suffix appended.
// Example: If ObjectMeta.Name is "test" and suffix is "object", the value of "test-object" will be returned.
func NameWithSuffix(meta metav1.ObjectMeta, suffix string) string {
return fmt.Sprintf("%s-%s", meta.Name, suffix)
}

func newEvent(meta metav1.ObjectMeta) *corev1.Event {
event := &corev1.Event{}
event.ObjectMeta.GenerateName = fmt.Sprintf("%s-", meta.Name)
event.ObjectMeta.Labels = meta.Labels
event.ObjectMeta.Namespace = meta.Namespace
return event
}

// LabelsForCluster returns the labels for all cluster resources.
func LabelsForCluster(cr *argoproj.ArgoCD) map[string]string {
labels := common.DefaultLabels(cr.Name)
return labels
}

// annotationsForCluster returns the annotations for all cluster resources.
func AnnotationsForCluster(cr *argoproj.ArgoCD) map[string]string {
annotations := common.DefaultAnnotations(cr.Name, cr.Namespace)
for key, val := range cr.ObjectMeta.Annotations {
annotations[key] = val
}
return annotations
}
68 changes: 68 additions & 0 deletions controllers/argoutil/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2019 ArgoCD Operator Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package argoutil

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
)

// FetchSecret will retrieve the object with the given Name using the provided client.
// The result will be returned.
func FetchSecret(client client.Client, meta metav1.ObjectMeta, name string) (*corev1.Secret, error) {
a := &argoproj.ArgoCD{}
a.ObjectMeta = meta
secret := NewSecretWithName(a, name)
return secret, FetchObject(client, meta.Namespace, name, secret)
}

// NewTLSSecret returns a new TLS Secret based on the given metadata with the provided suffix on the Name.
func NewTLSSecret(cr *argoproj.ArgoCD, suffix string) *corev1.Secret {
secret := NewSecretWithSuffix(cr, suffix)
secret.Type = corev1.SecretTypeTLS
return secret
}

// NewSecret returns a new Secret based on the given metadata.
func NewSecret(cr *argoproj.ArgoCD) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Labels: LabelsForCluster(cr),
},
Type: corev1.SecretTypeOpaque,
}
}

// NewSecretWithName returns a new Secret based on the given metadata with the provided Name.
func NewSecretWithName(cr *argoproj.ArgoCD, name string) *corev1.Secret {
secret := NewSecret(cr)

secret.ObjectMeta.Name = name
secret.ObjectMeta.Namespace = cr.Namespace
secret.ObjectMeta.Labels[common.ArgoCDKeyName] = name

return secret
}

// NewSecretWithSuffix returns a new Secret based on the given metadata with the provided suffix on the Name.
func NewSecretWithSuffix(cr *argoproj.ArgoCD, suffix string) *corev1.Secret {
return NewSecretWithName(cr, fmt.Sprintf("%s-%s", cr.Name, suffix))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-logr/logr v1.2.4
github.com/google/go-cmp v0.5.9
github.com/json-iterator/go v1.1.12
github.com/onsi/ginkgo v1.16.5
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.27.10
github.com/openshift/api v3.9.1-0.20190916204813-cdbe64fb0c91+incompatible
github.com/openshift/client-go v0.0.0-20200325131901-f7baeb993edb
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1499,9 +1499,8 @@ github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk=
Expand Down

0 comments on commit 6a6847a

Please sign in to comment.