func JoinLabelMaps(m1, m2 map[string]string) map[string]string {
	for key, value := range m2 {
		m1[key] = value
	}
	return m1
}
JoinLabelMaps adds a label to CR
package utils
import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"strings"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"github.com/go-logr/logr"
	awsv1alpha1 "github.com/openshift/aws-account-operator/pkg/apis/aws/v1alpha1"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/rand"
	"k8s.io/apimachinery/pkg/util/sets"
)
const (
	Finalizer = "finalizer.aws.managed.openshift.io"
	WaitTime = 25
	envDevMode = "FORCE_DEV_MODE"
	SREAssumeRole = "RH-SREP-CCS-Access"
	AwsSecretName = "aws-account-operator-credentials"
)
var operatorStartTime metav1.Time
func InitOperatorStartTime() error {
	if !operatorStartTime.IsZero() {
		return errors.New("Attempt to initialize start time twice")
	}
	operatorStartTime = metav1.Now()
	return nil
}
func GetOperatorStartTime() *metav1.Time {
	return &operatorStartTime
}
type awsStatement struct {
	Effect    string                 `json:"Effect"`
	Action    []string               `json:"Action"`
	Resource  []string               `json:"Resource,omitempty"`
	Condition *awsv1alpha1.Condition `json:"Condition,omitempty"`
	Principal *awsv1alpha1.Principal `json:"Principal,omitempty"`
}
type devMode string
const (
	DevModeProduction devMode = ""
	DevModeLocal devMode = "local"
	DevModeCluster devMode = "cluster"
)
var DetectDevMode devMode = devMode(strings.ToLower(os.Getenv(envDevMode)))
type awsPolicy struct {
	Version   string
	Statement []awsStatement
}
func MarshalIAMPolicy(role awsv1alpha1.AWSFederatedRole) (string, error) {
	statements := []awsStatement{}
	for _, statement := range role.Spec.AWSCustomPolicy.Statements {
		statements = append(statements, awsStatement(statement))
	}
	policyDoc := awsPolicy{
		Version:   "2012-10-17",
		Statement: statements,
	}
	jsonPolicyDoc, err := json.Marshal(&policyDoc)
	if err != nil {
		return "", err
	}
	return string(jsonPolicyDoc), nil
}
func AddFinalizer(object metav1.Object, finalizer string) {
	finalizers := sets.NewString(object.GetFinalizers()...)
	finalizers.Insert(finalizer)
	object.SetFinalizers(finalizers.List())
}
func AddLabels(object metav1.Object, labels map[string]string) {
	existingLabels := object.GetLabels()
	object.SetLabels(JoinLabelMaps(labels, existingLabels))
}
func LogAwsError(logger logr.Logger, errMsg string, customError error, err error) {
	if aerr, ok := err.(awserr.Error); ok {
		if customError == nil {
			customError = aerr
		}
		logger.Error(customError,
			fmt.Sprintf(`%s,
				AWS Error Code: %s,
				AWS Error Message: %s`,
				errMsg,
				aerr.Code(),
				aerr.Message()))
	}
}
func Contains(list []string, s string) bool {
	for _, v := range list {
		if v == s {
			return true
		}
	}
	return false
}
func Remove(list []string, s string) []string {
	for i, v := range list {
		if v == s {
			list = append(list[:i], list[i+1:]...)
		}
	}
	return list
}
func GenerateShortUID() string {
	return rand.String(6)
}
func GenerateLabel(key, value string) map[string]string {
	return map[string]string{key: value}
}