Skip to content

Commit

Permalink
comments per review so far.
Browse files Browse the repository at this point in the history
Have not restested yet
  • Loading branch information
jlmorris3827 committed Feb 9, 2019
1 parent ae54426 commit 6dacb37
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 334 deletions.
51 changes: 28 additions & 23 deletions mexos/inst.go → mexos/appinst.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func MEXAppCreateAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
log.DebugLog(log.DebugLevelMexos, "mex create app inst", "rootlb", rootLB.Name, "clusterinst", clusterInst, "appinst", appInst)

appDeploymentType := app.Deployment
clusterName := clusterInst.Key.ClusterKey.Name
appName := NormalizeName(app.Key.Name)
operatorName := NormalizeName(appInst.Key.CloudletKey.OperatorKey.Name)

//TODO values.application.template
var kubeNames KubeNames
err := GetKubeNames(clusterInst, app, appInst, &kubeNames)
if err != nil {
log.DebugLog(log.DebugLevelMexos, "GetKubeNames failed")
return err
}

if CloudletIsLocalDIND() {
masteraddr := dind.GetMasterAddr()
Expand All @@ -32,17 +33,17 @@ func MEXAppCreateAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,

if len(portDetail) > 0 {
log.DebugLog(log.DebugLevelMexos, "call AddNginxProxy for dind", "ports", portDetail)
if err := AddNginxProxy("localhost", appName, masteraddr, portDetail, dind.GetDockerNetworkName(clusterName)); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot add nginx proxy", "name", appName, "ports", portDetail)
if err := AddNginxProxy("localhost", kubeNames.appName, masteraddr, portDetail, dind.GetDockerNetworkName(kubeNames.clusterName)); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot add nginx proxy", "appName", kubeNames.appName, "ports", portDetail)
return err
}
}

log.DebugLog(log.DebugLevelMexos, "call runKubectlCreateApp for dind")
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
err = runKubectlCreateApp(clusterInst, appInst, rootLB, app.DeploymentManifest)
err = runKubectlCreateApp(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
err = CreateHelmAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
err = CreateHelmAppInst(rootLB, &kubeNames, appInst, clusterInst, app.DeploymentManifest)
} else {
err = fmt.Errorf("invalid deployment type %s for dind", appDeploymentType)
}
Expand All @@ -53,12 +54,12 @@ func MEXAppCreateAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
return nil
}

switch operatorName {
switch kubeNames.operatorName {
case cloudcommon.OperatorGCP:
fallthrough
case cloudcommon.OperatorAzure:
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
return runKubectlCreateApp(clusterInst, appInst, rootLB, app.DeploymentManifest)
return runKubectlCreateApp(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
} else if appDeploymentType == cloudcommon.AppDeploymentTypeKVM {
return fmt.Errorf("not yet supported")
} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
Expand All @@ -69,10 +70,10 @@ func MEXAppCreateAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
return fmt.Errorf("unknown deployment type %s", appDeploymentType)
default:
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
return CreateKubernetesAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
return CreateKubernetesAppInst(rootLB, &kubeNames, clusterInst, appInst, app.DeploymentManifest)

} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
return CreateHelmAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
return CreateHelmAppInst(rootLB, &kubeNames, appInst, clusterInst, app.DeploymentManifest)
}
//TODO -- support these later
//} else if appDeploymentType == cloudcommon.AppDeploymentTypeKVM {
Expand All @@ -87,17 +88,21 @@ func MEXAppCreateAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
func MEXAppDeleteAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst, app *edgeproto.App, appInst *edgeproto.AppInst) error {
log.DebugLog(log.DebugLevelMexos, "mex delete app inst", "rootlb", rootLB.Name, "clusterinst", clusterInst, "appinst", appInst)
appDeploymentType := app.Deployment
operatorName := NormalizeName(appInst.Key.CloudletKey.OperatorKey.Name)
appName := NormalizeName(app.Key.Name)
var kubeNames KubeNames
err := GetKubeNames(clusterInst, app, appInst, &kubeNames)
if err != nil {
log.DebugLog(log.DebugLevelMexos, "GetKubeNames failed")
return err
}

if CloudletIsLocalDIND() {
log.DebugLog(log.DebugLevelMexos, "run kubectl delete app for dind")

var err error
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
err = runKubectlDeleteApp(clusterInst, appInst, rootLB, app.DeploymentManifest)
err = runKubectlDeleteApp(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
err = DeleteHelmAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
err = DeleteHelmAppInst(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
} else {
err = fmt.Errorf("invalid deployment type %s for dind", appDeploymentType)
}
Expand All @@ -112,20 +117,20 @@ func MEXAppDeleteAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
return err
}
if len(portDetail) > 0 {
if err = DeleteNginxProxy("localhost", appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot delete nginx proxy", "name", appName)
if err = DeleteNginxProxy("localhost", kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot delete nginx proxy", "name", kubeNames.appName)
return err
}
}
return nil
}

switch operatorName {
switch kubeNames.operatorName {
case cloudcommon.OperatorGCP:
fallthrough
case cloudcommon.OperatorAzure:
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
return runKubectlDeleteApp(clusterInst, appInst, rootLB, app.DeploymentManifest)
return runKubectlDeleteApp(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
} else if appDeploymentType == cloudcommon.AppDeploymentTypeKVM {
return fmt.Errorf("not yet supported")
} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
Expand All @@ -136,9 +141,9 @@ func MEXAppDeleteAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst,
return fmt.Errorf("unknown image type %s", appDeploymentType)
default:
if appDeploymentType == cloudcommon.AppDeploymentTypeKubernetes {
return DeleteKubernetesAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
return DeleteKubernetesAppInst(rootLB, &kubeNames, clusterInst)
} else if appDeploymentType == cloudcommon.AppDeploymentTypeHelm {
return DeleteHelmAppInst(rootLB, clusterInst, app.DeploymentManifest, app, appInst)
return DeleteHelmAppInst(rootLB, &kubeNames, clusterInst, app.DeploymentManifest)
}
//TODO
//} else if appDeploymentType == cloudcommon.AppDeploymentTypeKVM {
Expand Down
10 changes: 6 additions & 4 deletions mexos/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/mobiledgex/edge-cloud/log"
)

func GetResourceGroupForCluster(clusterInst *edgeproto.ClusterInst) string {
return clusterInst.Key.CloudletKey.Name + "_" + clusterInst.Key.ClusterKey.Name
}

func azureCreateAKS(clusterInst *edgeproto.ClusterInst) error {
var err error
resourceGroup := GetResourceGroupForCluster(clusterInst)
Expand All @@ -26,10 +30,8 @@ func azureCreateAKS(clusterInst *edgeproto.ClusterInst) error {
if err = azure.GetAKSCredentials(resourceGroup, clusterName); err != nil {
return err
}
kconf, err := GetKconf(clusterInst, false) // XXX
if err != nil {
return fmt.Errorf("cannot get kconf, %v, %v, %v", clusterInst, kconf, err)
}
kconf := GetKconfName(clusterInst) // XXX

log.DebugLog(log.DebugLevelMexos, "warning, using default config") //XXX
//XXX watch out for multiple cluster contexts
if err = copyFile(defaultKubeconfig(), kconf); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions mexos/cloudletinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ import (
var CloudletInfra *edgeproto.CloudletInfraProperties
var CloudletInfraCommon *edgeproto.CloudletInfraCommon

//this is possible actions and optional parameters
var actionChoices = map[string]string{
"CLOUDLET_KIND": "procname",
"stop": "procname",
"status": "procname",
"ctrlapi": "procname",
"ctrlcli": "procname",
"ctrlinfo": "procname",
"dmeapi": "procname",
"deploy": "",
"cleanup": "",
"fetchlogs": "",
"createcluster": "",
"deletecluster": "",
"gencerts": "",
"cleancerts": "",
"sleep": "seconds",
}

func InitializeCloudletInfra(fakecloudlet bool) error {
log.DebugLog(log.DebugLevelMexos, "InitializeCloudletInfra called")

Expand Down
6 changes: 2 additions & 4 deletions mexos/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ func gcloudCreateGKE(clusterInst *edgeproto.ClusterInst) error {
if err = gcloud.GetGKECredentials(clusterName); err != nil {
return err
}
kconf, err := GetKconf(clusterInst, false) //XXX
if err != nil {
return fmt.Errorf("cannot get kconf, %v, %v", clusterInst, err)
}
kconf := GetKconfName(clusterInst) //XXX

log.DebugLog(log.DebugLevelMexos, "warning, using default config") //XXX
if err = copyFile(defaultKubeconfig(), kconf); err != nil {
return fmt.Errorf("can't copy %s, %v", defaultKubeconfig(), err)
Expand Down
36 changes: 15 additions & 21 deletions mexos/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,34 @@ import (
"github.com/mobiledgex/edge-cloud/log"
)

func DeleteHelmAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst, kubeManifest string, app *edgeproto.App, appInst *edgeproto.AppInst) error {
func DeleteHelmAppInst(rootLB *MEXRootLB, kubeNames *KubeNames, clusterInst *edgeproto.ClusterInst, kubeManifest string) error {
log.DebugLog(log.DebugLevelMexos, "delete kubernetes helm app")

appName := NormalizeName(app.Key.Name)
clusterName := clusterInst.Key.ClusterKey.Name

var err error
if rootLB == nil {
return fmt.Errorf("cannot delete helm app, rootLB is null")
}
kp, err := ValidateKubernetesParameters(clusterInst, rootLB, clusterName)
kp, err := ValidateKubernetesParameters(rootLB, kubeNames, clusterInst)
if err != nil {
return err
}
if CloudletIsLocalDIND() {
// remove DNS entries
if err = deleteAppDNS(kp, appInst.Uri, appName); err != nil {
if err = deleteAppDNS(kp, kubeNames.appURI, kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "warning, cannot delete DNS record", "error", err)
}
} else {
// remove DNS entries
if err = KubeDeleteDNSRecords(rootLB, kp, appInst.Uri, appName); err != nil {
if err = KubeDeleteDNSRecords(rootLB, kp, kubeNames.appURI, kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "warning, cannot delete DNS record", "error", err)
}
// remove Security rules
if err = DeleteProxySecurityRules(rootLB, kp.ipaddr, appInst); err != nil {
if err = DeleteProxySecurityRules(rootLB, kp.ipaddr, kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "warning, cannot delete security rules", "error", err)
}
}

cmd := fmt.Sprintf("%s helm delete --purge %s", kp.kubeconfig, appName)
cmd := fmt.Sprintf("%s helm delete --purge %s", kp.kubeconfig, kubeNames.appName)
out, err := kp.client.Output(cmd)
if err != nil {
return fmt.Errorf("error deleting helm chart, %s, %s, %v", cmd, out, err)
Expand All @@ -47,18 +44,15 @@ func DeleteHelmAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst, ku
return nil
}

func CreateHelmAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst, kubeManifest string, app *edgeproto.App, appInst *edgeproto.AppInst) error {
log.DebugLog(log.DebugLevelMexos, "create kubernetes helm app", "clusterInst", clusterInst, "appInst", appInst)
func CreateHelmAppInst(rootLB *MEXRootLB, kubeNames *KubeNames, appInst *edgeproto.AppInst, clusterInst *edgeproto.ClusterInst, kubeManifest string) error {
log.DebugLog(log.DebugLevelMexos, "create kubernetes helm app", "clusterInst", clusterInst, "kubeNames", kubeNames)

clusterName := clusterInst.Key.ClusterKey.Name
appName := NormalizeName(app.Key.Name)
appImage := NormalizeName(app.ImagePath)
var err error

if rootLB == nil {
return fmt.Errorf("cannot create helm app, rootLB is null")
}
kp, err := ValidateKubernetesParameters(clusterInst, rootLB, clusterName)
kp, err := ValidateKubernetesParameters(rootLB, kubeNames, clusterInst)
if err != nil {
return err
}
Expand Down Expand Up @@ -96,32 +90,32 @@ func CreateHelmAppInst(rootLB *MEXRootLB, clusterInst *edgeproto.ClusterInst, ku
// XXX This gets helm's prometheus able to query kubelet metrics.
// This can be removed once Lev passes in an option in the yaml to
// set the helm command line options.
prom, err := regexp.MatchString("prometheus", appName)
prom, err := regexp.MatchString("prometheus", kubeNames.appName)
if err == nil && prom {
log.DebugLog(log.DebugLevelMexos, "setting helm prometheus option")
helmOpts = "--set kubelet.serviceMonitor.https=true"
}
cmd = fmt.Sprintf("%s helm install %s --name %s %s", kp.kubeconfig, appImage, appName, helmOpts)
cmd = fmt.Sprintf("%s helm install %s --name %s %s", kp.kubeconfig, kubeNames.appImage, kubeNames.appName, helmOpts)
out, err = kp.client.Output(cmd)
if err != nil {
return fmt.Errorf("error deploying helm chart, %s, %s, %v", cmd, out, err)
}
log.DebugLog(log.DebugLevelMexos, "applied helm chart")
if CloudletIsLocalDIND() {
// Add DNS Zone
if err = createAppDNS(kp, appInst.Uri, appName); err != nil {
if err = createAppDNS(kp, kubeNames.appURI, kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot add DNS entries", "error", err)
return err
}
} else {
// Add security rules
if err = AddProxySecurityRules(rootLB, kp.ipaddr, appInst); err != nil {
if err = AddProxySecurityRules(rootLB, kp.ipaddr, kubeNames.appName, appInst); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot create security rules", "error", err)
return err
}
log.DebugLog(log.DebugLevelMexos, "done AddProxySecurityRules", "app", app)
log.DebugLog(log.DebugLevelMexos, "done AddProxySecurityRules", "kubeNames.appName", kubeNames.appName)
// Add DNS Zone
if err = KubeAddDNSRecords(rootLB, kp, appInst.Uri, appName); err != nil {
if err = KubeAddDNSRecords(rootLB, kp, appInst.Uri, kubeNames.appName); err != nil {
log.DebugLog(log.DebugLevelMexos, "cannot add DNS entries", "error", err)
return err
}
Expand Down
6 changes: 2 additions & 4 deletions mexos/kubeadmdind.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ func localCreateDIND(clusterInst *edgeproto.ClusterInst) error {
//race condition exists where the config file is not ready until just after the cluster create is done
time.Sleep(3 * time.Second)

kconf, err := GetKconf(clusterInst, false) // XXX
if err != nil {
return fmt.Errorf("cannot get kconf, %v, %v", kconf, err)
}
kconf := GetKconfName(clusterInst) // XXX

log.DebugLog(log.DebugLevelMexos, "warning, using default config") //XXX
//XXX watch out for multiple cluster contexts
if err = copyFile(defaultKubeconfig(), kconf); err != nil {
Expand Down
47 changes: 23 additions & 24 deletions mexos/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,37 @@ func GetKconfName(clusterInst *edgeproto.ClusterInst) string {
GetCloudletDNSZone())
}

func GetKconf(clusterInst *edgeproto.ClusterInst, createIfMissing bool) (string, error) {
func GetKconf(clusterInst *edgeproto.ClusterInst) (string, error) {
name := GetLocalKconfName(clusterInst)
operatorName := clusterInst.Key.CloudletKey.OperatorKey.Name
clusterName := clusterInst.Key.ClusterKey.Name

log.DebugLog(log.DebugLevelMexos, "get kubeconfig name", "name", name)
if createIfMissing { // XXX
if _, err := os.Stat(name); os.IsNotExist(err) {
// if kubeconfig does not exist, optionally create it. It is possible it was
// created on a different container or we had a restart of the container
log.DebugLog(log.DebugLevelMexos, "creating missing kconf file", "name", name)
switch operatorName {
case cloudcommon.OperatorGCP:
if err = gcloud.GetGKECredentials(clusterName); err != nil {
return "", fmt.Errorf("unable to get GKE credentials %v", err)
}
if err = copyFile(defaultKubeconfig(), name); err != nil {
return "", fmt.Errorf("can't copy %s, %v", defaultKubeconfig(), err)
}
case cloudcommon.OperatorAzure:
rg := GetResourceGroupForCluster(clusterInst)
if err = azure.GetAKSCredentials(rg, clusterName); err != nil {
return "", fmt.Errorf("unable to get AKS credentials %v", err)
}
if err = copyFile(defaultKubeconfig(), name); err != nil {
return "", fmt.Errorf("can't copy %s, %v", defaultKubeconfig(), err)
}
default:
log.DebugLog(log.DebugLevelMexos, "warning, not creating missing kubeconfig for operator", "operator", operatorName)
if _, err := os.Stat(name); os.IsNotExist(err) {
// if kubeconfig does not exist, optionally create it. It is possible it was
// created on a different container or we had a restart of the container
log.DebugLog(log.DebugLevelMexos, "creating missing kconf file", "name", name)
switch operatorName {
case cloudcommon.OperatorGCP:
if err = gcloud.GetGKECredentials(clusterName); err != nil {
return "", fmt.Errorf("unable to get GKE credentials %v", err)
}
if err = copyFile(defaultKubeconfig(), name); err != nil {
return "", fmt.Errorf("can't copy %s, %v", defaultKubeconfig(), err)
}
case cloudcommon.OperatorAzure:
rg := GetResourceGroupForCluster(clusterInst)
if err = azure.GetAKSCredentials(rg, clusterName); err != nil {
return "", fmt.Errorf("unable to get AKS credentials %v", err)
}
if err = copyFile(defaultKubeconfig(), name); err != nil {
return "", fmt.Errorf("can't copy %s, %v", defaultKubeconfig(), err)
}
default:
log.DebugLog(log.DebugLevelMexos, "warning, not creating missing kubeconfig for operator", "operator", operatorName)
}
}

return name, nil
}

Expand Down
Loading

0 comments on commit 6dacb37

Please sign in to comment.