Created
October 8, 2021 11:32
-
-
Save praveen4g0/4dac7a7b64edea249e0d96d6f1b631a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package backend | |
import ( | |
"context" | |
"fmt" | |
"strings" | |
"gopkg.in/yaml.v2" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
) | |
type ClusterStatus int | |
const ( | |
CLUSTER_STATUS_INVALID ClusterStatus = 0 | |
CLUSTER_STATUS_ACTIVE ClusterStatus = 1 | |
CLUSTER_STATUS_INACTIVE ClusterStatus = 2 | |
) | |
// ClusterInfoRequest is cluster_info create request body | |
type ClusterInfoRequest struct { | |
Cluster ClusterInfoBody `json:"clusterInfoBody"` | |
} | |
// ClusterInfoBody is the spec for creating/updating new cluster information | |
type ClusterInfoBody struct { | |
Id string `json:"cluster_id,omitempty"` | |
Status ClusterStatus `json:"status"` | |
Location ClusterLocation `json:"location,omitempty"` | |
Cni string `json:"cni,omitempty"` | |
IpSubnet string `json:"ip_subnet,omitempty"` | |
PrometheusUrl string `json:"prometheus_url,omitempty"` | |
AggregatedPrometheusUrl string `json:"aggregated_prometheus_url,omitempty"` | |
} | |
type ClusterLocation struct { | |
CloudProvider string `json:"cloud_vendor,omitempty"` | |
Region string `json:"region,omitempty"` | |
Latitude string `json:"latitude,omitempty"` | |
Longitude string `json:"longitude,omitempty"` | |
} | |
func GetClusterLocationInfo(ctx context.Context, client *kubernetes.Clientset, prometheus_url, aggrigated_prometheus_url, latitude, longitude string) (ClusterInfoRequest, error) { | |
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) | |
if err != nil { | |
return ClusterInfoRequest{}, fmt.Errorf("can't find cluster information: %+v ", err) | |
} | |
var location ClusterLocation | |
if nodes.Items[0].Spec.ProviderID != "" { | |
location = ClusterLocation{ | |
CloudProvider: strings.Split(nodes.Items[0].Spec.ProviderID, ":")[0], | |
Region: nodes.Items[0].ObjectMeta.Labels["topology.kubernetes.io/region"], | |
Latitude: latitude, | |
Longitude: longitude, | |
} | |
} | |
excluded_prefixes, err := getNsmExcludedPrefix(ctx, client) | |
if err != nil { | |
log.Error(err, "couldn't get excluded ipsubnet") | |
} | |
return ClusterInfoRequest{ | |
Cluster: ClusterInfoBody{ | |
Status: CLUSTER_STATUS_ACTIVE, | |
Location: location, | |
IpSubnet: strings.Join(excluded_prefixes, ","), | |
PrometheusUrl: prometheus_url, | |
AggregatedPrometheusUrl: aggrigated_prometheus_url, | |
}, | |
}, nil | |
} | |
func getNsmExcludedPrefix(ctx context.Context, client *kubernetes.Clientset) ([]string, error) { | |
nsnmconfig, err := client.CoreV1().ConfigMaps("avesha-system").Get(context.TODO(), "nsm-config", metav1.GetOptions{}) | |
if err != nil { | |
return nil, fmt.Errorf("can't get configmap nsm-config from avesha-system namespace: %+v ", err) | |
} | |
var cmData map[string]interface{} | |
err = yaml.Unmarshal([]byte(nsnmconfig.Data["excluded_prefixes.yaml"]), &cmData) | |
if err != nil { | |
return nil, fmt.Errorf("yaml unmarshalling error: %+v ", err) | |
} | |
if value, ok := cmData["prefixes"]; ok { | |
prefixes := make([]string, len(value.([]interface{}))) | |
for i, v := range value.([]interface{}) { | |
prefixes[i] = fmt.Sprint(v) | |
} | |
return prefixes, nil | |
} | |
return nil, fmt.Errorf("error occured getting excluded prefixes") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment