// lets find the executable on the PATH or in the fabric8 directory func resolveBinaryLocation(executable string) string { path, err := exec.LookPath(executable) if err != nil || fileNotExist(path) { home := os.Getenv("HOME") if home == "" { util.Error("No $HOME environment variable found") } writeFileLocation := getFabric8BinLocation() // lets try in the fabric8 folder path = filepath.Join(writeFileLocation, executable) if fileNotExist(path) { path = executable // lets try in the folder where we found the gofabric8 executable folder, err := osext.ExecutableFolder() if err != nil { util.Errorf("Failed to find executable folder: %v\n", err) } else { path = filepath.Join(folder, executable) if fileNotExist(path) { util.Infof("Could not find executable at %v\n", path) path = executable } } } } util.Infof("using the executable %s\n", path) return path }
func ensureNamespaceExists(c *k8sclient.Client, oc *oclient.Client, ns string) error { typeOfMaster := util.TypeOfMaster(c) if typeOfMaster == util.Kubernetes { nss := c.Namespaces() _, err := nss.Get(ns) if err != nil { // lets assume it doesn't exist! util.Infof("Creating new Namespace: %s\n", ns) entity := kapi.Namespace{ ObjectMeta: kapi.ObjectMeta{Name: ns}, } _, err := nss.Create(&entity) return err } } else { _, err := oc.Projects().Get(ns) if err != nil { // lets assume it doesn't exist! request := projectapi.ProjectRequest{ ObjectMeta: kapi.ObjectMeta{Name: ns}, } util.Infof("Creating new Project: %s\n", ns) _, err := oc.ProjectRequests().Create(&request) return err } } return nil }
func createMissingPVs(c *k8sclient.Client, ns string) { found, pvcs, pendingClaimNames := findPendingPVs(c, ns) if found { sshCommand := "" createPV(c, ns, pendingClaimNames, sshCommand) items := pvcs.Items for _, item := range items { status := item.Status.Phase if status == api.ClaimPending || status == api.ClaimLost { err := c.PersistentVolumeClaims(ns).Delete(item.ObjectMeta.Name) if err != nil { util.Infof("Error deleting PVC %s\n", item.ObjectMeta.Name) } else { util.Infof("Recreating PVC %s\n", item.ObjectMeta.Name) c.PersistentVolumeClaims(ns).Create(&api.PersistentVolumeClaim{ ObjectMeta: api.ObjectMeta{ Name: item.ObjectMeta.Name, Namespace: ns, }, Spec: api.PersistentVolumeClaimSpec{ VolumeName: ns + "-" + item.ObjectMeta.Name, AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, Resources: api.ResourceRequirements{ Requests: api.ResourceList{ api.ResourceName(api.ResourceStorage): resource.MustParse("1Gi"), }, }, }, }) } } } } }
func printSummary(typeOfMaster util.MasterType, externalNodeName string, ns string, domain string, c *k8sclient.Client) { util.Info("\n") util.Info("-------------------------\n") util.Info("\n") clientType := getClientTypeName(typeOfMaster) if externalNodeName != "" { util.Info("Deploying ingress controller on node ") util.Successf("%s", externalNodeName) util.Info(" use its external ip when configuring your wildcard DNS.\n") util.Infof("To change node move the label: `%s label node %s %s- && %s label node $YOUR_NEW_NODE %s=true`\n", clientType, externalNodeName, externalIPLabel, clientType, externalIPLabel) util.Info("\n") } util.Info("Default GOGS admin username/password = "******"%s/%s\n", gogsDefaultUsername, gogsDefaultPassword) util.Info("\n") found, _ := checkIfPVCsPending(c, ns) if found { util.Errorf("There are pending PersistentVolumeClaims\n") util.Infof("If using a local cluster run `gofabric8 volumes` to create missing HostPath volumes\n") util.Infof("If using a remote cloud then enable dynamic persistence with a StorageClass. For details see http://fabric8.io/guide/getStarted/persistence.html\n") util.Info("\n") } util.Infof("Downloading images and waiting to open the fabric8 console...\n") util.Info("\n") util.Info("-------------------------\n") }
func runTemplate(c *k8sclient.Client, oc *oclient.Client, appToRun string, ns string, domain string, apiserver string, pv bool) { util.Info("\n\nInstalling: ") util.Successf("%s\n\n", appToRun) typeOfMaster := util.TypeOfMaster(c) if typeOfMaster == util.Kubernetes { jsonData, format, err := loadTemplateData(ns, appToRun, c, oc) if err != nil { printError("Failed to load app "+appToRun, err) } createTemplate(jsonData, format, appToRun, ns, domain, apiserver, c, oc, pv) } else { tmpl, err := oc.Templates(ns).Get(appToRun) if err != nil { printError("Failed to load template "+appToRun, err) } util.Infof("Loaded template with %d objects", len(tmpl.Objects)) processTemplate(tmpl, ns, domain, apiserver) objectCount := len(tmpl.Objects) util.Infof("Creating "+appToRun+" template resources from %d objects\n", objectCount) for _, o := range tmpl.Objects { err = processItem(c, oc, &o, ns, pv) } } }
func NewCmdVolumes(f *cmdutil.Factory) *cobra.Command { cmd := &cobra.Command{ Use: "volumes", Short: "Creates a persisent volume for any pending persistance volume claims", Long: `Creates a persisent volume for any pending persistance volume claims`, PreRun: func(cmd *cobra.Command, args []string) { showBanner() }, Run: func(cmd *cobra.Command, args []string) { c, _ := client.NewClient(f) ns, _, err := f.DefaultNamespace() if err != nil { util.Fatal("No default namespace") } else { found, pvcs, pendingClaimNames := findPendingPVs(c, ns) if found { sshCommand := cmd.Flags().Lookup(sshCommandFlag).Value.String() createPV(c, ns, pendingClaimNames, sshCommand) items := pvcs.Items for _, item := range items { name := item.ObjectMeta.Name status := item.Status.Phase if status == api.ClaimPending || status == api.ClaimLost { err = c.PersistentVolumeClaims(ns).Delete(name) if err != nil { util.Infof("Error deleting PVC %s\n", name) } else { util.Infof("Recreating PVC %s\n", name) c.PersistentVolumeClaims(ns).Create(&api.PersistentVolumeClaim{ ObjectMeta: api.ObjectMeta{ Name: name, Namespace: ns, }, Spec: api.PersistentVolumeClaimSpec{ VolumeName: ns + "-" + name, AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, Resources: api.ResourceRequirements{ Requests: api.ResourceList{ api.ResourceName(api.ResourceStorage): resource.MustParse("1Gi"), }, }, }, }) } } } } } }, } cmd.PersistentFlags().String(sshCommandFlag, "", "the ssh command to run commands inside the VM of the single node cluster") return cmd }
func createPV(c *k8sclient.Client, ns string, pvcNames []string, sshCommand string) (Result, error) { for _, pvcName := range pvcNames { hostPath := path.Join("/data", ns, pvcName) nsPvcName := ns + "-" + pvcName pvs := c.PersistentVolumes() rc, err := pvs.List(api.ListOptions{}) if err != nil { util.Errorf("Failed to load PersistentVolumes with error %v\n", err) } items := rc.Items for _, volume := range items { if nsPvcName == volume.ObjectMeta.Name { util.Infof("Already created PersistentVolumes for %s\n", nsPvcName) } } // we no longer need to do chmod on kubernetes as we have init containers now typeOfMaster := util.TypeOfMaster(c) if typeOfMaster != util.Kubernetes || len(sshCommand) > 0 { err = configureHostPathVolume(c, ns, hostPath, sshCommand) if err != nil { util.Errorf("Failed to configure the host path %s with error %v\n", hostPath, err) } } // lets create a new PV util.Infof("PersistentVolume name %s will be created on host path %s\n", nsPvcName, hostPath) pv := api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: nsPvcName, }, Spec: api.PersistentVolumeSpec{ Capacity: api.ResourceList{ api.ResourceName(api.ResourceStorage): resource.MustParse("1Gi"), }, AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{Path: hostPath}, }, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRecycle, }, } _, err = pvs.Create(&pv) if err != nil { util.Errorf("Failed to create PersistentVolume %s at %s with error %v\n", nsPvcName, hostPath, err) } } return Success, nil }
func generateSshKeyPair(logGeneratedKeys string) Keypair { priv, err := rsa.GenerateKey(rand.Reader, 2014) if err != nil { util.Fatalf("Error generating key", err) } err = priv.Validate() if err != nil { util.Fatalf("Validation failed.", err) } // Get der format. priv_der []byte priv_der := x509.MarshalPKCS1PrivateKey(priv) // pem.Block // blk pem.Block priv_blk := pem.Block{ Type: "RSA PRIVATE KEY", Headers: nil, Bytes: priv_der, } // Resultant private key in PEM format. // priv_pem string priv_pem := string(pem.EncodeToMemory(&priv_blk)) if logGeneratedKeys == "true" { util.Infof(priv_pem) } // Public Key generation pub := priv.PublicKey pub_der, err := x509.MarshalPKIXPublicKey(&pub) if err != nil { util.Fatalf("Failed to get der format for PublicKey.", err) } pub_blk := pem.Block{ Type: "PUBLIC KEY", Headers: nil, Bytes: pub_der, } pub_pem := string(pem.EncodeToMemory(&pub_blk)) if logGeneratedKeys == "true" { util.Infof(pub_pem) } return Keypair{ pub: []byte(pub_pem), priv: []byte(priv_pem), } }
func createPersistentVolume(cmd *cobra.Command, ns string, c *k8sclient.Client, fac *cmdutil.Factory) (Result, error) { flags := cmd.Flags() hostPath := flags.Lookup(hostPathFlag).Value.String() name := flags.Lookup(nameFlag).Value.String() pvs := c.PersistentVolumes() rc, err := pvs.List(labels.Everything(), fields.Everything()) if err != nil { util.Errorf("Failed to load PersistentVolumes with error %v", err) return Failure, err } items := rc.Items for _, volume := range items { // TODO use the external load balancer as a way to know if we should create a route? vname := volume.ObjectMeta.Name if vname == name { util.Infof("Already created PersistentVolumes for %s\n", name) return Success, nil } } if hostPath == "" { return missingFlag(cmd, hostPathFlag) } if confirmAction(flags) == false { return Failure, nil } // lets create a new PV util.Infof("PersistentVolume name %s will be created on host path %s\n", name, hostPath) pv := api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: name, }, Spec: api.PersistentVolumeSpec{ Capacity: api.ResourceList{ api.ResourceName(api.ResourceStorage): resource.MustParse("100G"), }, AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteMany}, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{Path: hostPath}, }, }, } _, err = pvs.Create(&pv) if err != nil { util.Errorf("Failed to create PersistentVolume %s at %s with error %v", name, hostPath, err) return Failure, err } return Success, nil }
func downloadKubectlClient() (err error) { os := runtime.GOOS arch := runtime.GOARCH kubectlBinary := kubectl if runtime.GOOS == "windows" { kubectlBinary += ".exe" } _, err = exec.LookPath(kubectlBinary) if err != nil { latestVersion, err := getLatestVersionFromGitHub(kubernetes, kubernetes) if err != nil { return fmt.Errorf("Unable to get latest version for %s/%s %v", kubernetes, kubernetes, err) } clientURL := fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/%s/%s", latestVersion, os, arch, kubectlBinary) util.Infof("Downloading %s...\n", clientURL) fullPath := filepath.Join(getFabric8BinLocation(), kubectlBinary) err = downloadFile(fullPath, clientURL) if err != nil { util.Errorf("Unable to download file %s/%s %v", fullPath, clientURL, err) return err } util.Successf("Downloaded %s\n", fullPath) } else { util.Successf("%s is already available on your PATH\n", kubectlBinary) } return nil }
func downloadKubernetes(d downloadProperties) (err error) { os := runtime.GOOS arch := runtime.GOARCH if runtime.GOOS == "windows" { d.kubeBinary += ".exe" } _, err = exec.LookPath(d.kubeBinary) if err != nil { latestVersion, err := getLatestVersionFromGitHub(d.kubeDistroOrg, d.kubeDistroRepo) if err != nil { util.Errorf("Unable to get latest version for %s/%s %v", d.kubeDistroOrg, d.kubeDistroRepo, err) return err } kubeURL := fmt.Sprintf(d.downloadURL+d.kubeDistroRepo+"/releases/"+d.extraPath+"v%s/%s-%s-%s", latestVersion, d.kubeDistroRepo, os, arch) if runtime.GOOS == "windows" { kubeURL += ".exe" } util.Infof("Downloading %s...\n", kubeURL) fullPath := filepath.Join(getFabric8BinLocation(), d.kubeBinary) err = downloadFile(fullPath, kubeURL) if err != nil { util.Errorf("Unable to download file %s/%s %v", fullPath, kubeURL, err) return err } util.Successf("Downloaded %s\n", fullPath) } else { util.Successf("%s is already available on your PATH\n", d.kubeBinary) } return nil }
func waitForDeployment(c *k8sclient.Client, ns string, name string, sleepMillis time.Duration) error { util.Infof("Deployment %s waiting for it to be ready...\n", name) for { deployment, err := c.Extensions().Deployments(ns).Get(name) if err != nil { return err } available := deployment.Status.AvailableReplicas unavailable := deployment.Status.UnavailableReplicas if unavailable == 0 && available > 0 { util.Infof("DeploymentConfig %s now has %d available replicas\n", name, available) return nil } time.Sleep(sleepMillis) } }
func copyEndpoints(c *k8sclient.Client, fromNs string, toNs string, names []string) error { var answer error for _, name := range names { item, err := c.Endpoints(fromNs).Get(name) if err != nil { util.Warnf("No endpoint called %s found in namespace %s", name, fromNs) answer = err } name := item.Name objectMeta := item.ObjectMeta current, err := c.Endpoints(toNs).Get(name) if current == nil || err != nil { // lets create the endpoint newEndpoints := &api.Endpoints{ ObjectMeta: api.ObjectMeta{ Name: name, Labels: objectMeta.Labels, Annotations: objectMeta.Annotations, }, Subsets: item.Subsets, } _, err = c.Endpoints(toNs).Create(newEndpoints) if err != nil { return err } util.Infof("Created endpoint %s in namespace %s\n", name, toNs) } } return answer }
func downloadFunktion() (err error) { os := runtime.GOOS arch := runtime.GOARCH _, err = exec.LookPath(funktion) if err != nil { latestVersion, err := getLatestVersionFromGitHub(fabric8io, funktionOperator) if err != nil { util.Errorf("Unable to get latest version for %s/%s %v", fabric8io, funktionOperator, err) return err } funktionURL := fmt.Sprintf(githubURL+fabric8io+"/"+funktionOperator+"/releases/download/v%s/%s-%s-%s", latestVersion, funktionOperator, os, arch) if runtime.GOOS == "windows" { funktionURL += ".exe" } util.Infof("Downloading %s...\n", funktionURL) fullPath := filepath.Join(getFabric8BinLocation(), funktion) err = downloadFile(fullPath, funktionURL) if err != nil { util.Errorf("Unable to download file %s/%s %v", fullPath, funktionURL, err) return err } util.Successf("Downloaded %s\n", fullPath) } else { util.Successf("%s is already available on your PATH\n", funktion) } return nil }
func processResource(c *k8sclient.Client, b []byte, ns string, kind string) error { util.Infof("Processing resource kind: %s in namespace %s\n", kind, ns) req := c.Post().Body(b) if kind == "Deployment" { req.AbsPath("apis", "extensions/v1beta1", "namespaces", ns, strings.ToLower(kind+"s")) } else if kind == "BuildConfig" || kind == "DeploymentConfig" || kind == "Template" || kind == "PolicyBinding" || kind == "Role" || kind == "RoleBinding" { req.AbsPath("oapi", "v1", "namespaces", ns, strings.ToLower(kind+"s")) } else if kind == "OAuthClient" || kind == "Project" || kind == "ProjectRequest" { req.AbsPath("oapi", "v1", strings.ToLower(kind+"s")) } else if kind == "Namespace" { req.AbsPath("api", "v1", "namespaces") } else { req.Namespace(ns).Resource(strings.ToLower(kind + "s")) } res := req.Do() if res.Error() != nil { err := res.Error() if err != nil { util.Warnf("Failed to create %s: %v", kind, err) return err } } var statusCode int res.StatusCode(&statusCode) if statusCode != http.StatusCreated { return fmt.Errorf("Failed to create %s: %d", kind, statusCode) } return nil }
func deployFabric8SASSecurityContextConstraints(c *k8sclient.Client, f *cmdutil.Factory, ns string) (Result, error) { name := Fabric8SASSCC scc := kapi.SecurityContextConstraints{ ObjectMeta: kapi.ObjectMeta{ Name: name, }, SELinuxContext: kapi.SELinuxContextStrategyOptions{ Type: kapi.SELinuxStrategyRunAsAny, }, RunAsUser: kapi.RunAsUserStrategyOptions{ Type: kapi.RunAsUserStrategyRunAsAny, }, Groups: []string{"system:serviceaccounts"}, Volumes: []kapi.FSType{kapi.FSTypeGitRepo, kapi.FSTypeConfigMap, kapi.FSTypeSecret, kapi.FSTypeEmptyDir}, } _, err := c.SecurityContextConstraints().Get(name) if err == nil { err = c.SecurityContextConstraints().Delete(name) if err != nil { return Failure, err } } _, err = c.SecurityContextConstraints().Create(&scc) if err != nil { util.Fatalf("Cannot create SecurityContextConstraints: %v\n", err) util.Fatalf("Failed to create SecurityContextConstraints %v in namespace %s: %v\n", scc, ns, err) return Failure, err } util.Infof("SecurityContextConstraints %s is setup correctly\n", name) return Success, err }
func downloadClient(isMinishift bool) (err error) { os := runtime.GOOS arch := runtime.GOARCH _, err = exec.LookPath(kubectl) if err != nil { latestVersion, err := getLatestVersionFromGitHub(kubeDistroOrg, kubernetes) if err != nil { return fmt.Errorf("Unable to get latest version for %s/%s %v", kubeDistroOrg, kubernetes, err) } if isMinishift { clientBinary = oc return fmt.Errorf("Openshift client download not yet supported") } clientURL := fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/v%s/bin/%s/%s/%s", latestVersion, os, arch, kubectl) util.Infof("Downloading %s...", clientURL) err = downloadFile(writeFileLocation+clientBinary, clientURL) if err != nil { util.Errorf("Unable to download file %s/%s %v", writeFileLocation+clientBinary, clientURL, err) return err } util.Successf("Downloaded %s\n", clientBinary) } else { util.Successf("%s is already available on your PATH\n", clientBinary) } return nil }
func processData(jsonData []byte, format string, templateName string, ns string, c *k8sclient.Client, oc *oclient.Client, pv bool) { // lets check if its an RC / ReplicaSet or something o, groupVersionKind, err := api.Codecs.UniversalDeserializer().Decode(jsonData, nil, nil) if err != nil { printResult(templateName, Failure, err) } else { kind := groupVersionKind.Kind //util.Infof("Processing resource of kind: %s version: %s\n", kind, groupVersionKind.Version) if len(kind) <= 0 { printResult(templateName, Failure, fmt.Errorf("Could not find kind from json %s", string(jsonData))) } else { accessor := meta.NewAccessor() ons, err := accessor.Namespace(o) if err == nil && len(ons) > 0 { util.Infof("Found namespace on kind %s of %s", kind, ons) ns = ons err := ensureNamespaceExists(c, oc, ns) if err != nil { printErr(err) } } if !pv { if kind == "PersistentVolumeClaim" { return } jsonData = removePVCVolumes(jsonData, format, templateName, kind) } err = processResource(c, jsonData, ns, kind) if err != nil { printResult(templateName, Failure, err) } } } }
func downloadKubernetes(isMinishift bool) (err error) { os := runtime.GOOS arch := runtime.GOARCH if isMinishift { kubeDistroOrg = minishiftOwner kubeDistroRepo = minishift kubeDownloadURL = minishiftDownloadURL downloadPath = "download/" kubeBinary = minishift } _, err = exec.LookPath(kubeBinary) if err != nil { latestVersion, err := getLatestVersionFromGitHub(kubeDistroOrg, kubeDistroRepo) if err != nil { util.Errorf("Unable to get latest version for %s/%s %v", kubeDistroOrg, kubeDistroRepo, err) return err } kubeURL := fmt.Sprintf(kubeDownloadURL+kubeDistroRepo+"/releases/"+downloadPath+"v%s/%s-%s-%s", latestVersion, kubeDistroRepo, os, arch) util.Infof("Downloading %s...", kubeURL) err = downloadFile(writeFileLocation+kubeBinary, kubeURL) if err != nil { util.Errorf("Unable to download file %s/%s %v", writeFileLocation+kubeBinary, kubeURL, err) return err } util.Successf("Downloaded %s\n", kubeBinary) } else { util.Successf("%s is already available on your PATH\n", kubeBinary) } return nil }
func createPV(c *k8sclient.Client, ns string, pvcNames []string, cmd *cobra.Command) (Result, error) { for _, pvcName := range pvcNames { hostPath := "/data/" + pvcName pvs := c.PersistentVolumes() rc, err := pvs.List(api.ListOptions{}) if err != nil { util.Errorf("Failed to load PersistentVolumes with error %v\n", err) } items := rc.Items for _, volume := range items { vname := volume.ObjectMeta.Name if vname == pvcName { util.Infof("Already created PersistentVolumes for %s\n", pvcName) } } err = configureHostPathVolume(c, ns, hostPath, cmd) if err != nil { util.Errorf("Failed to configure the host path %s with error %v\n", hostPath, err) } // lets create a new PV util.Infof("PersistentVolume name %s will be created on host path %s\n", pvcName, hostPath) pv := api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: pvcName, }, Spec: api.PersistentVolumeSpec{ Capacity: api.ResourceList{ api.ResourceName(api.ResourceStorage): resource.MustParse("5Gi"), }, AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce}, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{Path: hostPath}, }, }, } _, err = pvs.Create(&pv) if err != nil { util.Errorf("Failed to create PersistentVolume %s at %s with error %v\n", pvcName, hostPath, err) } } return Success, nil }
// Ensure that the `restricted` SecurityContextConstraints has the RunAsUser set to RunAsAny // // if `restricted does not exist lets create it // otherwise if needed lets modify the RunAsUser func verifyRestrictedSecurityContextConstraints(c *k8sclient.Client, f *cmdutil.Factory) (Result, error) { name := RestrictedSCC ns, _, e := f.DefaultNamespace() if e != nil { util.Fatal("No default namespace") return Failure, e } rc, err := c.SecurityContextConstraints().Get(name) if err != nil { scc := kapi.SecurityContextConstraints{ ObjectMeta: kapi.ObjectMeta{ Name: RestrictedSCC, }, SELinuxContext: kapi.SELinuxContextStrategyOptions{ Type: kapi.SELinuxStrategyMustRunAs, }, RunAsUser: kapi.RunAsUserStrategyOptions{ Type: kapi.RunAsUserStrategyRunAsAny, }, Groups: []string{bootstrappolicy.AuthenticatedGroup}, } _, err = c.SecurityContextConstraints().Create(&scc) if err != nil { return Failure, err } else { util.Infof("SecurityContextConstraints %s created\n", name) return Success, err } } // lets check that the restricted is configured correctly if kapi.RunAsUserStrategyRunAsAny != rc.RunAsUser.Type { rc.RunAsUser.Type = kapi.RunAsUserStrategyRunAsAny _, err = c.SecurityContextConstraints().Update(rc) if err != nil { util.Fatalf("Failed to update SecurityContextConstraints %v in namespace %s: %v\n", rc, ns, err) return Failure, err } util.Infof("SecurityContextConstraints %s is updated to enable fabric8\n", name) } else { util.Infof("SecurityContextConstraints %s is configured correctly\n", name) } return Success, err }
func processItem(c *k8sclient.Client, oc *oclient.Client, item *runtime.Object, ns string, pv bool) error { /* groupVersionKind, err := api.Scheme.ObjectKind(*item) if err != nil { return err } kind := groupVersionKind.Kind //kind := *item.GetObjectKind() util.Infof("Procesing kind %s\n", kind) b, err := json.Marshal(item) if err != nil { return err } return processResource(c, b, ns, kind) */ o := *item switch o := o.(type) { case *runtime.Unstructured: data := o.Object metadata := data["metadata"] switch metadata := metadata.(type) { case map[string]interface{}: namespace := metadata["namespace"] switch namespace := namespace.(type) { case string: //util.Infof("Custom namespace '%s'\n", namespace) if len(namespace) <= 0 { // TODO why is the namespace empty? // lets default the namespace to the default gogs namespace namespace = "user-secrets-source-admin" } ns = namespace // lets check that this new namespace exists err := ensureNamespaceExists(c, oc, ns) if err != nil { printErr(err) } } } //util.Infof("processItem %s with value: %#v\n", ns, o.Object) b, err := json.Marshal(o.Object) if err != nil { return err } if !pv { if o.Kind == "PersistentVolumeClaim" { return nil } b = removePVCVolumes(b, "json", o.Name, o.Kind) } return processResource(c, b, ns, o.TypeMeta.Kind) default: util.Infof("Unknown type %v\n", reflect.TypeOf(item)) } return nil }
func missingFlag(cmd *cobra.Command, name string) (Result, error) { util.Errorf("No option -%s specified!\n", hostPathFlag) text := cmd.Name() parent := cmd.Parent() if parent != nil { text = parent.Name() + " " + text } util.Infof("Please try something like: %s --%s='some value' ...\n\n", text, hostPathFlag) return Failure, nil }
func waitForDeploymentConfig(c *oclient.Client, ns string, name string, sleepMillis time.Duration) error { util.Infof("DeploymentConfig %s in namespace %s waiting for it to be ready...\n", name, ns) for { deployment, err := c.DeploymentConfigs(ns).Get(name) if err != nil { util.Warnf("Cannot find DeploymentConfig %s in namepsace %s due to %s\n", name, ns, err) return err } if deployment.Status.Replicas == 0 { util.Warnf("No replicas for DeploymentConfig %s in namespace %s\n", name, ns) } available := deployment.Status.AvailableReplicas unavailable := deployment.Status.UnavailableReplicas if unavailable == 0 && available > 0 { util.Infof("DeploymentConfig %s now has %d available replicas\n", name, available) return nil } //util.Infof("DeploymentConfig %s has %d available replicas and %d unavailable\n", name, available, unavailable) time.Sleep(sleepMillis) } }
func printSummary(typeOfMaster util.MasterType, externalNodeName string, mini bool, ns string, domain string) { util.Info("\n") util.Info("-------------------------\n") util.Info("\n") clientType := getClientTypeName(typeOfMaster) if externalNodeName != "" { util.Info("Deploying ingress controller on node ") util.Successf("%s", externalNodeName) util.Info(" use its external ip when configuring your wildcard DNS.\n") util.Infof("To change node move the label: `%s label node %s %s- && %s label node $YOUR_NEW_NODE %s=true`\n", clientType, externalNodeName, externalIPLabel, clientType, externalIPLabel) util.Info("\n") } util.Info("Default GOGS admin username/password = "******"%s/%s\n", gogsDefaultUsername, gogsDefaultPassword) util.Info("\n") util.Infof("Downloading images and waiting to open the fabric8 console...\n") util.Info("\n") util.Info("-------------------------\n") }
func deployFabric8SecurityContextConstraints(c *k8sclient.Client, f *cmdutil.Factory, ns string) (Result, error) { name := Fabric8SCC if ns != "default" { name += "-" + ns } var priority int32 = 10 scc := kapi.SecurityContextConstraints{ ObjectMeta: kapi.ObjectMeta{ Name: name, }, Priority: &priority, AllowPrivilegedContainer: true, AllowHostNetwork: true, AllowHostPorts: true, Volumes: []kapi.FSType{kapi.FSTypeAll}, SELinuxContext: kapi.SELinuxContextStrategyOptions{ Type: kapi.SELinuxStrategyRunAsAny, }, RunAsUser: kapi.RunAsUserStrategyOptions{ Type: kapi.RunAsUserStrategyRunAsAny, }, Users: []string{ "system:serviceaccount:openshift-infra:build-controller", "system:serviceaccount:" + ns + ":default", "system:serviceaccount:" + ns + ":fabric8", "system:serviceaccount:" + ns + ":gerrit", "system:serviceaccount:" + ns + ":jenkins", "system:serviceaccount:" + ns + ":router", "system:serviceaccount:" + ns + ":registry", "system:serviceaccount:" + ns + ":gogs", "system:serviceaccount:" + ns + ":fluentd", }, Groups: []string{bootstrappolicy.ClusterAdminGroup, bootstrappolicy.NodesGroup}, } _, err := c.SecurityContextConstraints().Get(name) if err == nil { err = c.SecurityContextConstraints().Delete(name) if err != nil { return Failure, err } } _, err = c.SecurityContextConstraints().Create(&scc) if err != nil { util.Errorf("Cannot create SecurityContextConstraints: %v\n", err) util.Errorf("Failed to create SecurityContextConstraints %v in namespace %s: %v\n", scc, ns, err) return Failure, err } util.Infof("SecurityContextConstraints %s is setup correctly\n", name) return Success, err }
func downloadDriver() (err error) { if runtime.GOOS == "darwin" { util.Infof("fabric8 recommends OSX users use the xhyve driver\n") info, err := exec.Command("brew", "info", "docker-machine-driver-xhyve").Output() if err != nil || strings.Contains(string(info), "Not installed") { e := exec.Command("brew", "install", "docker-machine-driver-xhyve") e.Stdout = os.Stdout e.Stderr = os.Stderr err = e.Run() if err != nil { return err } out, err := exec.Command("brew", "--prefix").Output() if err != nil { return err } brewPrefix := strings.TrimSpace(string(out)) file := string(brewPrefix) + "/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve" e = exec.Command("sudo", "chown", "root:wheel", file) e.Stdout = os.Stdout e.Stderr = os.Stderr err = e.Run() if err != nil { return err } e = exec.Command("sudo", "chmod", "u+s", file) e.Stdout = os.Stdout e.Stderr = os.Stderr err = e.Run() if err != nil { return err } util.Success("xhyve driver installed\n") } else { util.Success("xhyve driver already installed\n") } } else if runtime.GOOS == "linux" { return errors.New("Driver install for " + runtime.GOOS + " not yet supported") } return nil }
func NewCmdCopyEndpoints(f *cmdutil.Factory) *cobra.Command { cmd := &cobra.Command{ Use: "copy-endpoints", Short: "Copies endpoints from the current namespace to a target namespace", Long: `Copies endpoints from the current namespace to a target namespace`, PreRun: func(cmd *cobra.Command, args []string) { showBanner() }, Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { util.Info("Please specify one or more endpoint names to copy as arguments!\n") return } c, cfg := client.NewClient(f) oc, _ := client.NewOpenShiftClient(cfg) initSchema() toNamespace := cmd.Flags().Lookup(toNamespaceFlag).Value.String() fromNamespace := cmd.Flags().Lookup(fromNamespaceFlag).Value.String() if len(fromNamespace) == 0 { ns, _, err := f.DefaultNamespace() if err != nil { util.Fatal("No default namespace") } fromNamespace = ns } if len(toNamespace) == 0 { util.Fatal("No target namespace specified!") } util.Infof("Copying endpoints from namespace: %s to namespace: %s\n", fromNamespace, toNamespace) err := ensureNamespaceExists(c, oc, toNamespace) if err != nil { util.Fatalf("Failed to copy endpoints %v", err) } err = copyEndpoints(c, fromNamespace, toNamespace, args) if err != nil { util.Fatalf("Failed to copy endpoints %v", err) } }, } cmd.PersistentFlags().StringP(fromNamespaceFlag, "f", "", "the source namespace or uses the default namespace") cmd.PersistentFlags().StringP(toNamespaceFlag, "t", "", "the destination namespace") return cmd }
func watchAndWaitForDeploymentConfig(c *oclient.Client, ns string, name string, timeout time.Duration) error { if isDeploymentConfigAvailable(c, ns, name) { return nil } w, err := c.DeploymentConfigs(ns).Watch(api.ListOptions{}) if err != nil { return err } _, err = watch.Until(timeout, w, func(e watch.Event) (bool, error) { if e.Type == watch.Error { return false, fmt.Errorf("encountered error while watching DeploymentConfigs: %v", e.Object) } obj, isDC := e.Object.(*deployapi.DeploymentConfig) if !isDC { return false, fmt.Errorf("received unknown object while watching for DeploymentConfigs: %v", obj) } deployment := obj if deployment.Name == name { replicas := deployment.Status.Replicas available := deployment.Status.AvailableReplicas unavailable := deployment.Status.UnavailableReplicas if unavailable == 0 && available > 0 { util.Infof("DeploymentConfig %s now has %d available replicas\n", name, available) return true, nil } else { util.Infof("DeploymentConfig %s has %d replicas, %d available and %d unavailable\n", name, replicas, available, unavailable) } } return false, nil }) if err != nil { return err } return nil }
// if we are on minikube or minishift lets try to create the // hostPath folders with relaxed persmissions func configureHostPathVolume(c *k8sclient.Client, ns string, hostPath string, sshCommand string) error { cli := sshCommand if len(cli) == 0 { context, isMini, _ := util.GetMiniType() if isMini { cli = context } } if len(cli) == 0 { // lets default to using vagrant if we have a Vagrantfile if _, err := os.Stat("Vagrantfile"); err == nil { cli = "vagrant" } } if len(cli) == 0 { return nil } shellCommands := []string{ fmt.Sprintf("sudo mkdir -p %s", hostPath), fmt.Sprintf("sudo chmod 777 %s", hostPath), fmt.Sprintf("echo hostPath is setup correctly at: %s", hostPath), } util.Infof("About to modify host paths on the VM via the command: %s\n", cli) for _, shellCmd := range shellCommands { args := []string{"ssh", fmt.Sprintf("/bin/sh -c '%s'", shellCmd)} cmd := exec.Command(cli, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr var waitStatus syscall.WaitStatus if err := cmd.Run(); err != nil { printErr(err) if exitError, ok := err.(*exec.ExitError); ok { waitStatus = exitError.Sys().(syscall.WaitStatus) printStatus(waitStatus.ExitStatus()) } return err } else { waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus) printStatus(waitStatus.ExitStatus()) } } return nil }