|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + |
| 7 | + pluginTypes "github.com/argoproj/argo-rollouts/utils/plugin/types" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + v1 "k8s.io/api/core/v1" |
| 10 | + kubeErrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | + "k8s.io/client-go/tools/clientcmd" |
| 14 | +) |
| 15 | + |
| 16 | +func GetKubeConfig() (*rest.Config, error) { |
| 17 | + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() |
| 18 | + // if you want to change the loading rules (which files in which order), you can do so here |
| 19 | + configOverrides := &clientcmd.ConfigOverrides{} |
| 20 | + // if you want to change override values or bind them to flags, there are methods to help you |
| 21 | + kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides) |
| 22 | + config, err := kubeConfig.ClientConfig() |
| 23 | + if err != nil { |
| 24 | + return nil, pluginTypes.RpcError{ErrorString: err.Error()} |
| 25 | + } |
| 26 | + return config, nil |
| 27 | +} |
| 28 | + |
| 29 | +func SetLogLevel(logLevel string) { |
| 30 | + level, err := log.ParseLevel(logLevel) |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + log.SetLevel(level) |
| 35 | +} |
| 36 | + |
| 37 | +func CreateFormatter(logFormat string) log.Formatter { |
| 38 | + var formatType log.Formatter |
| 39 | + switch strings.ToLower(logFormat) { |
| 40 | + case "json": |
| 41 | + formatType = &log.JSONFormatter{} |
| 42 | + case "text": |
| 43 | + formatType = &log.TextFormatter{ |
| 44 | + FullTimestamp: true, |
| 45 | + } |
| 46 | + default: |
| 47 | + log.Infof("Unknown format: %s. Using text logformat", logFormat) |
| 48 | + formatType = &log.TextFormatter{ |
| 49 | + FullTimestamp: true, |
| 50 | + } |
| 51 | + } |
| 52 | + return formatType |
| 53 | +} |
| 54 | + |
| 55 | +func CreateConfigMap(name string, options CreateConfigMapOptions) (*v1.ConfigMap, error) { |
| 56 | + clientset := options.Clientset |
| 57 | + ctx := options.Ctx |
| 58 | + configMap, err := clientset.Get(ctx, name, metav1.GetOptions{}) |
| 59 | + if err != nil && !kubeErrors.IsNotFound(err) { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + if err == nil { |
| 63 | + return configMap, err |
| 64 | + } |
| 65 | + configMap.Name = name |
| 66 | + configMap, err = clientset.Create(ctx, configMap, metav1.CreateOptions{}) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return configMap, err |
| 71 | +} |
| 72 | + |
| 73 | +func GetConfigMapData(configMap *v1.ConfigMap, configMapKey string, destination any) error { |
| 74 | + if configMap.Data != nil && configMap.Data[configMapKey] != "" { |
| 75 | + err := json.Unmarshal([]byte(configMap.Data[configMapKey]), &destination) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func UpdateConfigMapData(configMap *v1.ConfigMap, configMapData any, options UpdateConfigMapOptions) error { |
| 84 | + clientset := options.Clientset |
| 85 | + rawConfigMapData, err := json.Marshal(configMapData) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if configMap.Data == nil { |
| 90 | + configMap.Data = make(map[string]string) |
| 91 | + } |
| 92 | + configMap.Data[options.ConfigMapKey] = string(rawConfigMapData) |
| 93 | + _, err = clientset.Update(options.Ctx, configMap, metav1.UpdateOptions{}) |
| 94 | + return err |
| 95 | +} |
| 96 | + |
| 97 | +func RemoveIndex[T any](original []T, index int) []T { |
| 98 | + result := original[:index] |
| 99 | + return append(result, original[index+1:]...) |
| 100 | +} |
| 101 | + |
| 102 | +func DoTransaction(logCtx *log.Entry, taskList ...Task) error { |
| 103 | + var err, reverseErr error |
| 104 | + for index, task := range taskList { |
| 105 | + err = task.Action() |
| 106 | + if err == nil { |
| 107 | + continue |
| 108 | + } |
| 109 | + logCtx.Error(err.Error()) |
| 110 | + for i := index - 1; i > -1; i-- { |
| 111 | + reverseErr = taskList[i].ReverseAction() |
| 112 | + if err != nil { |
| 113 | + return reverseErr |
| 114 | + } |
| 115 | + } |
| 116 | + return err |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments