-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joshua M. Dotson
committed
Oct 2, 2018
1 parent
cc90df9
commit 65f0d8c
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright © 2018 Cisco Systems, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/cisco-sso/kdk/pkg/kdk" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var kubesyncCmd = &cobra.Command{ | ||
Use: `kubesync`, | ||
Short: "Sync default KUBECONFIG to KDK", | ||
Long: "Sync default KUBECONFIG to KDK and tune Docker Kubernetes API hostname", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
kdk.Kubesync(CurrentKdkEnvConfig) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(kubesyncCmd) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright © 2018 Cisco Systems, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kdk | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func Kubesync(cfg KdkEnvConfig) { | ||
|
||
// Check if KDK container is running | ||
kdkRunning := false | ||
|
||
containers, err := cfg.DockerClient.ContainerList(cfg.Ctx, types.ContainerListOptions{All: true}) | ||
|
||
if err != nil { | ||
log.WithField("error", err).Fatal("Failed to list docker containers") | ||
} | ||
|
||
for _, container := range containers { | ||
for _, name := range container.Names { | ||
if name == "/"+cfg.ConfigFile.AppConfig.Name { | ||
if container.State == "running" { | ||
kdkRunning = true | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
// if KDK container is not running, start it and provision KDK user | ||
if !kdkRunning { | ||
log.Error("KDK is not currently running.") | ||
} | ||
|
||
kubeconfigHostPath := cfg.Home() + "/.kube/config" | ||
kubeconfigKDKPath := ".kube/docker-for-desktop.example.org" | ||
|
||
// Create ~/.kube directory inside KDK if it doesn't already exist. | ||
remoteCommand := "mkdir -p ~/.kubetest" | ||
if err = cfg.Exec(remoteCommand); err != nil { | ||
log.WithField("error", err).Fatal("Failed to mkdir in KDK container.") | ||
} | ||
|
||
// Sync default KUBECONFIG to KDK | ||
if err = cfg.SCPTo(kubeconfigHostPath, kubeconfigKDKPath); err != nil { | ||
log.WithField("error", err).Fatal("Failed to scp to KDK container.") | ||
} | ||
|
||
// Tune Docker for Desktop's Kubernetes API hostname in KUBECONFIG | ||
remoteCommand = "sed -i -e 's@[email protected]@g' -e 's@docker-for-desktop.*@docker-for-desktop.example.org@g' " + kubeconfigKDKPath | ||
if err = cfg.Exec(remoteCommand); err != nil { | ||
log.WithField("error", err).Fatal("Failed to transform KUBECONFIG in KDK container.") | ||
} | ||
log.Info("Docker for Desktop KUBECONFIG synchronized to KDK.") | ||
} |