refactoring

This commit is contained in:
Safwan
2025-07-15 20:08:23 +05:00
parent 8b257a3f0c
commit f69773c588
2 changed files with 75 additions and 35 deletions

View File

@@ -1,89 +0,0 @@
package util
import (
"runtime/debug"
"github.com/stakater/Reloader/internal/pkg/options"
)
type ReloaderOptions struct {
AutoReloadAll bool `json:"autoReloadAll"`
ConfigmapUpdateOnChangeAnnotation string `json:"configmapUpdateOnChangeAnnotation"`
SecretUpdateOnChangeAnnotation string `json:"secretUpdateOnChangeAnnotation"`
ReloaderAutoAnnotation string `json:"reloaderAutoAnnotation"`
IgnoreResourceAnnotation string `json:"ignoreResourceAnnotation"`
ConfigmapReloaderAutoAnnotation string `json:"configmapReloaderAutoAnnotation"`
SecretReloaderAutoAnnotation string `json:"secretReloaderAutoAnnotation"`
ConfigmapExcludeReloaderAnnotation string `json:"configmapExcludeReloaderAnnotation"`
SecretExcludeReloaderAnnotation string `json:"secretExcludeReloaderAnnotation"`
AutoSearchAnnotation string `json:"autoSearchAnnotation"`
SearchMatchAnnotation string `json:"searchMatchAnnotation"`
RolloutStrategyAnnotation string `json:"rolloutStrategyAnnotation"`
LogFormat string `json:"logFormat"`
LogLevel string `json:"logLevel"`
IsArgoRollouts string `json:"isArgoRollouts"`
ReloadStrategy string `json:"reloadStrategy"`
ReloadOnCreate string `json:"reloadOnCreate"`
ReloadOnDelete string `json:"reloadOnDelete"`
SyncAfterRestart bool `json:"syncAfterRestart"`
EnableHA bool `json:"enableHA"`
WebhookUrl string `json:"webhookUrl"`
}
func GetReloaderOptions() *ReloaderOptions {
return &ReloaderOptions{
AutoReloadAll: options.AutoReloadAll,
ConfigmapUpdateOnChangeAnnotation: options.ConfigmapUpdateOnChangeAnnotation,
SecretUpdateOnChangeAnnotation: options.SecretUpdateOnChangeAnnotation,
ReloaderAutoAnnotation: options.ReloaderAutoAnnotation,
IgnoreResourceAnnotation: options.IgnoreResourceAnnotation,
ConfigmapReloaderAutoAnnotation: options.ConfigmapReloaderAutoAnnotation,
SecretReloaderAutoAnnotation: options.SecretReloaderAutoAnnotation,
ConfigmapExcludeReloaderAnnotation: options.ConfigmapExcludeReloaderAnnotation,
SecretExcludeReloaderAnnotation: options.SecretExcludeReloaderAnnotation,
AutoSearchAnnotation: options.AutoSearchAnnotation,
SearchMatchAnnotation: options.SearchMatchAnnotation,
RolloutStrategyAnnotation: options.RolloutStrategyAnnotation,
LogFormat: options.LogFormat,
LogLevel: options.LogLevel,
IsArgoRollouts: options.IsArgoRollouts,
ReloadStrategy: options.ReloadStrategy,
ReloadOnCreate: options.ReloadOnCreate,
ReloadOnDelete: options.ReloadOnDelete,
SyncAfterRestart: options.SyncAfterRestart,
EnableHA: options.EnableHA,
WebhookUrl: options.WebhookUrl,
}
}
type BuildInfo struct {
GoVersion string `json:"goversion"`
Version string `json:"version"`
Checksum string `json:"checksum"`
VCSRevision string `json:"vcs.revision,omitempty"`
VCSModified string `json:"vcs.modified,omitempty"`
VCSTime string `json:"vcs.time,omitempty"`
}
func parseBuildInfo(info *debug.BuildInfo) *BuildInfo {
infoMap := make(map[string]string)
infoMap["goversion"] = info.GoVersion
infoMap["version"] = info.Main.Version
infoMap["checksum"] = info.Main.Sum
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" || setting.Key == "vcs.time" || setting.Key == "vcs.modified" {
infoMap[setting.Key] = setting.Value
}
}
metaInfo := &BuildInfo{
GoVersion: info.GoVersion,
Version: info.Main.Version,
Checksum: info.Main.Sum,
VCSRevision: infoMap["vcs.revision"],
VCSModified: infoMap["vcs.modified"],
VCSTime: infoMap["vcs.time"],
}
return metaInfo
}

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
@@ -17,6 +16,7 @@ import (
"github.com/stakater/Reloader/internal/pkg/constants"
"github.com/stakater/Reloader/internal/pkg/crypto"
"github.com/stakater/Reloader/internal/pkg/options"
"github.com/stakater/Reloader/pkg/metainfo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@@ -78,43 +78,20 @@ func PublishMetaInfoConfigmap(clientset kubernetes.Interface) {
return
}
metaInfoMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "reloader-meta-info",
metaInfo := &metainfo.MetaInfo{
BuildInfo: *metainfo.NewBuildInfo(info),
ReloaderOptions: *metainfo.GetReloaderOptions(),
DeploymentInfo: metav1.ObjectMeta{
Name: os.Getenv("RELOADER_DEPLOYMENT_NAME"),
Namespace: namespace,
Labels: map[string]string{
"reloader.stakater.com/meta-info-for": "reloader-oss",
},
},
Data: map[string]string{},
}
buildInfo := parseBuildInfo(info)
buildInfoJSON, err := json.Marshal(buildInfo)
configMap := metaInfo.ToConfigMap()
if err == nil {
metaInfoMap.Data["buildinfo"] = string(buildInfoJSON)
}
reloaderOptions := GetReloaderOptions()
reloaderOptionsJSON, err := json.Marshal(reloaderOptions)
if err == nil {
metaInfoMap.Data["reloaderOptions"] = string(reloaderOptionsJSON)
}
deploymentInfoJson, err := json.Marshal(metav1.ObjectMeta{
Name: os.Getenv("RELOADER_DEPLOYMENT_NAME"),
Namespace: namespace,
})
if err == nil {
metaInfoMap.Data["deploymentInfo"] = string(deploymentInfoJson)
}
if _, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), metaInfoMap.Name, metav1.GetOptions{}); err == nil {
if _, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), configMap.Name, metav1.GetOptions{}); err == nil {
logrus.Info("Meta info configmap already exists, deleting it")
err = clientset.CoreV1().ConfigMaps(namespace).Delete(context.Background(), metaInfoMap.Name, metav1.DeleteOptions{})
err = clientset.CoreV1().ConfigMaps(namespace).Delete(context.Background(), configMap.Name, metav1.DeleteOptions{})
if err != nil {
logrus.Warn("Failed to delete existing meta info configmap: ", err)
return
@@ -122,7 +99,7 @@ func PublishMetaInfoConfigmap(clientset kubernetes.Interface) {
logrus.Info("Deleted existing meta info configmap")
}
_, err = clientset.CoreV1().ConfigMaps(namespace).Create(context.Background(), metaInfoMap, metav1.CreateOptions{})
_, err := clientset.CoreV1().ConfigMaps(namespace).Create(context.Background(), configMap, metav1.CreateOptions{})
if err != nil {
logrus.Warn("Failed to create meta info configmap: ", err)
}