Add/update/delete PrometheusRemoteEndpoint CR
[demo.git] / vnfs / DAaaS / microservices / remote-config-operator / pkg / controller / utils / remoteconfigutils.go
1 /*
2 Copyright 2019 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package utils
15
16 import (
17         "context"
18         "fmt"
19         "os"
20         onapv1alpha1 "remote-config-operator/pkg/apis/onap/v1alpha1"
21         "sigs.k8s.io/controller-runtime/pkg/client"
22 )
23
24 const (
25         defaultWatchLabel     = "remote=m3db1"
26         WatchLabelsEnvVar     = "WATCH_LABELS"
27         RemoteConfigFinalizer = "finalizer.remoteConfig.onap.org"
28 )
29
30 // GetWatchLabels returns the labels the operator should be watching for changes
31 func GetWatchLabels() (string, error) {
32         labelSelector, found := os.LookupEnv(WatchLabelsEnvVar)
33         if !found {
34                 return defaultWatchLabel, fmt.Errorf("%s must be set", WatchLabelsEnvVar)
35         }
36         return labelSelector, nil
37 }
38
39 // GetPrometheusRemoteEndpoint returns the prometheusRemoteEndpoint instance in the namespace ns
40 func GetPrometheusRemoteEndpoint(rc client.Client, ns string) (*onapv1alpha1.PrometheusRemoteEndpoint, error) {
41         // Get the PrometheusRemoteEndpoint instance in current namespace to rebuild conf.
42         preList := &onapv1alpha1.PrometheusRemoteEndpointList{}
43         preOpts := []client.ListOption{
44                 client.InNamespace("edge1"),
45                 client.MatchingLabels{"remote": "m3db1"},
46         }
47
48         err := rc.List(context.TODO(), preList, preOpts...)
49         if err != nil {
50                 return nil, err
51         }
52         if preList.Items == nil || len(preList.Items) == 0 {
53                 return nil, err
54         }
55         prometheusRemoteEndpoint := &preList.Items[0]
56         return prometheusRemoteEndpoint, nil
57 }
58
59 // Contains checks if a string is contained in a list of strings
60 func Contains(list []string, s string) bool {
61         for _, v := range list {
62                 if v == s {
63                         return true
64                 }
65         }
66         return false
67 }
68
69 // Remove checks and removes a string from a list of strings
70 func Remove(list []string, s string) []string {
71         for i, v := range list {
72                 if v == s {
73                         list = append(list[:i], list[i+1:]...)
74                 }
75         }
76         return list
77 }