//Determines the path of kubectl on the host func (p *Kubernetes) findKubeCtlPath() (string, error) { if p.DryRun() { return "/usr/bin/kubectl", nil } //Insert some additional test paths to try p.addCLIPaths( "/usr/bin/kubectl", "/usr/local/bin/kubectl", ) for _, path := range p.CLIPath() { //Add /host as a prefix if InContainer set to true if p.InContainer { path = filepath.Join("/host", path) } if utils.PathExists(path) { logrus.Debugf("Found kubectl at %s", path) return path, nil } } logrus.Errorf("Unable to find valid kubectl installation. Tried the following locations: %v", p.CLIPath()) return "", errors.New("No kubectl installation found") }
//Returns an *os.File pointing to the answers file. If one does not exist, it is created and returned. func (b *Base) createAnswersFile(sampleAnswersPath string) (*os.File, error) { if utils.PathExists(sampleAnswersPath) { return os.OpenFile(sampleAnswersPath, os.O_APPEND|os.O_WRONLY, 0600) } sampleAnswersFile, err := os.Create(sampleAnswersPath) if err != nil { logrus.Fatalf("Unable to create sample answers file: %s", err) return nil, err } return sampleAnswersFile, nil }
//LoadAnswers Unmarshals the answers from the answers.conf file into the base AnswersData func (b *Base) LoadAnswers() error { //if a directory was provided... fp := b.AnswersDir() if utils.PathIsDirectory(fp) { //Construct the full path by combining with the ANSWERS_FILE constant fp = filepath.Join(fp, constants.ANSWERS_FILE) if !utils.PathExists(fp) { return errors.New("Failed to read answers from path") } } //..try to parse the file if !utils.PathExists(fp) { return errors.New("Bad answers filepath") } p := parser.NewParser(fp) err := p.Unmarshal(&b.AnswersData) if err != nil { return errors.New("Failed to parse file") } return nil }
//Generates a new work directory //Returns a path to the directory func makeWorkDirectory(targetPath string) (string, error) { workdir := filepath.Join(targetPath, constants.WORKDIR) //If the .workdir directory does not exist in targetPath, make it. if !utils.PathExists(workdir) { logrus.Debugf("Making workdir in %s", targetPath) err := os.MkdirAll(workdir, 0700) if err != nil { logrus.Fatalf("Failed to make work directory in %s", targetPath) return "", errors.New("Failed to make work directory") } } return workdir, nil }
//Unmarshal arses a markup file into the given interface object func (p *Parser) Unmarshal(result interface{}) error { if !utils.PathExists(p.markupFile) { return errors.New("File does not exist") } f, err := ioutil.ReadFile(p.markupFile) if err != nil { return err } if isJSON(f) { return json.Unmarshal(f, result) } return yaml.Unmarshal(f, result) }
//deletePod removes the kubernetes pod func (p *Kubernetes) deletePod(artifactPath string) error { if !utils.PathExists(artifactPath) { logrus.Errorf("No valid artifact could be found at %s", artifactPath) return errors.New("Path does not exist") } namespaceFlag := fmt.Sprintf("--namespace=%s", p.Namespace) deleteCmd := exec.Command(p.KubeCtl, "delete", "-f", artifactPath, namespaceFlag) //In a dry run, we don't actually execute the commands, so we log and return here if p.DryRun() { logrus.Infof("DRY RUN: %s\n", deleteCmd.Args) return nil } if _, err := utils.CheckCommandOutput(deleteCmd, false); err != nil { return err } return nil }