github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/application/applications/monitoring/helm.go (about)

     1  package monitoring
     2  
     3  import (
     4  	"path/filepath"
     5  	"sort"
     6  
     7  	"github.com/caos/orbos/pkg/secret/read"
     8  
     9  	toolsetslatest "github.com/caos/orbos/internal/operator/boom/api/latest"
    10  	"github.com/caos/orbos/internal/operator/boom/application/applications/monitoring/auth"
    11  	"github.com/caos/orbos/internal/operator/boom/application/applications/monitoring/config"
    12  	"github.com/caos/orbos/internal/operator/boom/application/applications/monitoring/helm"
    13  	"github.com/caos/orbos/internal/operator/boom/templator/helm/chart"
    14  	"github.com/caos/orbos/internal/utils/helper"
    15  	"github.com/caos/orbos/internal/utils/kubectl"
    16  	"github.com/caos/orbos/internal/utils/kustomize"
    17  	"github.com/caos/orbos/mntr"
    18  )
    19  
    20  func (g *Grafana) HelmMutate(monitor mntr.Monitor, toolsetCRDSpec *toolsetslatest.ToolsetSpec, resultFilePath string) error {
    21  	if toolsetCRDSpec.KubeMetricsExporter != nil && toolsetCRDSpec.KubeMetricsExporter.Deploy &&
    22  		toolsetCRDSpec.MetricsPersisting != nil && (toolsetCRDSpec.MetricsPersisting.Metrics == nil || toolsetCRDSpec.MetricsPersisting.Metrics.KubeStateMetrics) {
    23  
    24  		if err := helper.DeleteFirstResourceFromYaml(resultFilePath, "v1", "ConfigMap", "grafana-persistentvolumesusage"); err != nil {
    25  			return err
    26  		}
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  func (g *Grafana) HelmPreApplySteps(monitor mntr.Monitor, spec *toolsetslatest.ToolsetSpec) ([]interface{}, error) {
    33  	config := config.New(spec)
    34  
    35  	folders := make([]string, 0)
    36  	for _, provider := range config.DashboardProviders {
    37  		folders = append(folders, provider.Folder)
    38  	}
    39  
    40  	outs, err := getKustomizeOutput(folders)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	ret := make([]interface{}, len(outs))
    46  	for k, v := range outs {
    47  		ret[k] = v
    48  	}
    49  	return ret, nil
    50  }
    51  
    52  type ProviderSorter []*helm.Provider
    53  
    54  func (a ProviderSorter) Len() int           { return len(a) }
    55  func (a ProviderSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    56  func (a ProviderSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
    57  
    58  type AlphaSorter []string
    59  
    60  func (a AlphaSorter) Len() int           { return len(a) }
    61  func (a AlphaSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    62  func (a AlphaSorter) Less(i, j int) bool { return a[i] < a[j] }
    63  
    64  func (g *Grafana) SpecToHelmValues(monitor mntr.Monitor, toolset *toolsetslatest.ToolsetSpec) interface{} {
    65  	version, err := kubectl.NewVersion().GetKubeVersion(monitor)
    66  	if err != nil {
    67  		return nil
    68  	}
    69  
    70  	imageTags := g.GetImageTags()
    71  	if toolset != nil && toolset.Monitoring != nil {
    72  		image := "grafana/grafana"
    73  		helper.OverwriteExistingValues(imageTags, map[string]string{
    74  			image: toolset.Monitoring.OverwriteVersion,
    75  		})
    76  		helper.OverwriteExistingKey(imageTags, &image, toolset.Monitoring.OverwriteImage)
    77  	}
    78  	values := helm.DefaultValues(imageTags)
    79  	conf := config.New(toolset)
    80  
    81  	values.KubeTargetVersionOverride = version
    82  
    83  	providers := make([]*helm.Provider, 0)
    84  	dashboards := make(map[string]string, 0)
    85  	datasources := make([]*helm.Datasource, 0)
    86  
    87  	//internal datasources
    88  	if conf.Datasources != nil {
    89  		for _, datasource := range conf.Datasources {
    90  			valuesDatasource := &helm.Datasource{
    91  				Name:      datasource.Name,
    92  				Type:      datasource.Type,
    93  				URL:       datasource.Url,
    94  				Access:    datasource.Access,
    95  				IsDefault: datasource.IsDefault,
    96  			}
    97  			datasources = append(datasources, valuesDatasource)
    98  		}
    99  	}
   100  
   101  	//internal dashboards
   102  	if conf.DashboardProviders != nil {
   103  		for _, provider := range conf.DashboardProviders {
   104  			sort.Sort(AlphaSorter(provider.ConfigMaps))
   105  			for _, configmap := range provider.ConfigMaps {
   106  				providers = append(providers, getProvider(configmap))
   107  				dashboards[configmap] = configmap
   108  			}
   109  		}
   110  	}
   111  
   112  	if len(providers) > 0 {
   113  		sort.Sort(ProviderSorter(providers))
   114  		values.Grafana.DashboardProviders = &helm.DashboardProviders{
   115  			Providers: &helm.Providersyaml{
   116  				APIVersion: 1,
   117  				Providers:  providers,
   118  			},
   119  		}
   120  
   121  		values.Grafana.DashboardsConfigMaps = dashboards
   122  	}
   123  	if len(datasources) > 0 {
   124  		values.Grafana.AdditionalDataSources = datasources
   125  	}
   126  
   127  	spec := toolset.Monitoring
   128  
   129  	if spec == nil {
   130  		return values
   131  	}
   132  
   133  	if spec.Admin != nil {
   134  		admin := spec.Admin
   135  		user, err := read.GetSecretValueOnlyIncluster(admin.Username, admin.ExistingUsername)
   136  		if err != nil || user == "" {
   137  			user = "admin"
   138  		}
   139  		values.Grafana.AdminUser = user
   140  
   141  		pw, err := read.GetSecretValueOnlyIncluster(admin.Password, admin.ExistingPassword)
   142  		if err != nil || pw == "" {
   143  			pw = "admin"
   144  		}
   145  		values.Grafana.AdminPassword = pw
   146  	}
   147  
   148  	if spec.Storage != nil {
   149  		values.Grafana.Persistence.Enabled = true
   150  		values.Grafana.Persistence.Size = spec.Storage.Size
   151  		values.Grafana.Persistence.StorageClassName = spec.Storage.StorageClass
   152  
   153  		if spec.Storage.AccessModes != nil {
   154  			values.Grafana.Persistence.AccessModes = spec.Storage.AccessModes
   155  		}
   156  	}
   157  
   158  	if spec.Network != nil && spec.Network.Domain != "" {
   159  		values.Grafana.Env["GF_SERVER_DOMAIN"] = spec.Network.Domain
   160  
   161  		if spec.Auth != nil {
   162  			if spec.Auth.Google != nil {
   163  				google, err := auth.GetGoogleAuthConfig(spec.Auth.Google)
   164  				if err == nil && google != nil {
   165  					values.Grafana.Ini.AuthGoogle = google
   166  				}
   167  			}
   168  
   169  			if spec.Auth.Github != nil {
   170  				github, err := auth.GetGithubAuthConfig(spec.Auth.Github)
   171  				if err == nil && github != nil {
   172  					values.Grafana.Ini.AuthGithub = github
   173  				}
   174  			}
   175  
   176  			if spec.Auth.Gitlab != nil {
   177  				gitlab, err := auth.GetGitlabAuthConfig(spec.Auth.Gitlab)
   178  				if err == nil && gitlab != nil {
   179  					values.Grafana.Ini.AuthGitlab = gitlab
   180  				}
   181  			}
   182  
   183  			if spec.Auth.GenericOAuth != nil {
   184  				generic, err := auth.GetGenericOAuthConfig(spec.Auth.GenericOAuth)
   185  				if err == nil && generic != nil {
   186  					values.Grafana.Ini.AuthGeneric = generic
   187  				}
   188  			}
   189  		}
   190  	}
   191  
   192  	if spec.Plugins != nil && len(spec.Plugins) > 0 {
   193  		values.Grafana.Plugins = append(values.Grafana.Plugins, spec.Plugins...)
   194  	}
   195  
   196  	if spec.NodeSelector != nil {
   197  		for k, v := range spec.NodeSelector {
   198  			values.Grafana.NodeSelector[k] = v
   199  		}
   200  	}
   201  
   202  	if spec.Tolerations != nil {
   203  		for idx := range spec.Tolerations {
   204  			tol := spec.Tolerations[idx]
   205  			values.Grafana.Tolerations = append(values.Grafana.Tolerations, tol)
   206  		}
   207  	}
   208  
   209  	if spec.Resources != nil {
   210  		values.Grafana.Resources = spec.Resources
   211  	}
   212  
   213  	return values
   214  }
   215  
   216  func getKustomizeOutput(folders []string) ([]string, error) {
   217  	ret := make([]string, len(folders))
   218  	for n, folder := range folders {
   219  
   220  		cmd, err := kustomize.New(folder)
   221  		if err != nil {
   222  			return nil, err
   223  		}
   224  		execcmd := cmd.Build()
   225  
   226  		out, err := execcmd.Output()
   227  		if err != nil {
   228  			return nil, err
   229  		}
   230  		ret[n] = string(out)
   231  	}
   232  	return ret, nil
   233  }
   234  
   235  func getProvider(configmapName string) *helm.Provider {
   236  	return &helm.Provider{
   237  		Name:            configmapName,
   238  		Type:            "file",
   239  		DisableDeletion: false,
   240  		Editable:        false,
   241  		Options: map[string]string{
   242  			"path": filepath.Join("/var/lib/grafana/dashboards", configmapName),
   243  		},
   244  	}
   245  }
   246  
   247  func (g *Grafana) GetChartInfo() *chart.Chart {
   248  	return helm.GetChartInfo()
   249  }
   250  
   251  func (g *Grafana) GetImageTags() map[string]string {
   252  	return helm.GetImageTags()
   253  }