deploys := make(map[string]string)
for _, c := range configs {
if isRecover {
log.Debugf("start to reset mock server ns[%s] deployment[%s]", c.Namespace, c.DeploymentName)
err = reset(k8s, c.Namespace, c.DeploymentName)
if err != nil {
handleErr(err, "Failed to reset the mock server. deployment=%s namespace=%s", c.Namespace, c.DeploymentN
ame)
}
deploys[c.DeploymentName] = c.Namespace
} else {
err = addSecrets(k8s,c.Namespace)
if err != nil {
handleErr(err, "Failed to add Secrets to the namespace %s", c.Namespace)
}
log.Debugf("start to inject mock server ns[%s] deployment[%s] dport[%s] mockfile[%s]", c.Namespace, c.De
ploymentName, c.Dport, c.MockFilePath)
err = injectMockServer(k8s, c.Namespace, c.DeploymentName, c.Dport, c.MockFilePath)
if err != nil {
handleErr(err, "Failed to setup the mock server. deployment=%s namespace=%s", c.Namespace, c.DeploymentN
ame)
}
deploys[c.DeploymentName] = c.Namespace
}
}
err = waitDeploymentReady(k8s, deploys)
if err != nil {
fmt.Printf("err: %+v\n", err)
os.Exit(1)
}
log.Info("Done")
}
func handleErr(err error, message string, v ...interface{}) {
err = errors.WithMessagef(err, message, v)
fmt.Printf("err: %+v\n", err)
os.Exit(1)
}
func reset(k8s *kubernetes.Clientset, ns string, deploymentName string) error {
deployment, err := k8s.AppsV1().Deployments(ns).Get(deploymentName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "Failed to get deployment")
}
initContainers := deployment.Spec.Template.Spec.InitContainers
for index, i := range initContainers {
if i.Name == "mock-init" {
initContainers = append(initContainers[:index], initContainers[index+1:]...)
}
}
deployment.Spec.Template.Spec.InitContainers = initContainers
Containers := deployment.Spec.Template.Spec.Containers
for index, i := range Containers {
if i.Name == "mock-server" {
Containers = append(Containers[:index], Containers[index+1:]...)
}
}
deployment.Spec.Template.Spec.Containers = Containers
_, err = k8s.AppsV1().Deployments(ns).Update(deployment)
if err != nil {
return errors.Wrap(err, "Failed to update deployment")
}
return nil
}
func injectMockServer(k8s *kubernetes.Clientset, ns string, deploymentName string, dport string, mockFil
ePath string) error {
deployment, err := k8s.AppsV1().Deployments(ns).Get(deploymentName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "Failed to get deployment")
}