Merge pull request #102560 from navist2020/todo/checkManifest

kubeadm:Run preflight checks for diff to check that the manifests already exist
This commit is contained in:
Kubernetes Prow Robot 2021-06-08 23:03:47 -07:00 committed by GitHub
commit 7696a48612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package upgrade
import (
"io"
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"
@ -65,7 +66,13 @@ func newCmdDiff(out io.Writer) *cobra.Command {
Use: "diff [version]",
Short: "Show what differences would be applied to existing static pod manifests. See also: kubeadm upgrade apply --dry-run",
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: Run preflight checks for diff to check that the manifests already exist.
// Run preflight checks for diff to check that the manifests already exist.
if err := validateManifestsPath(
flags.apiServerManifestPath,
flags.controllerManagerManifestPath,
flags.schedulerManifestPath); err != nil {
return err
}
return runDiff(flags, args)
},
}
@ -80,6 +87,25 @@ func newCmdDiff(out io.Writer) *cobra.Command {
return cmd
}
func validateManifestsPath(manifests ...string) (err error) {
for _, manifestPath := range manifests {
if len(manifestPath) == 0 {
return errors.New("empty manifest path")
}
s, err := os.Stat(manifestPath)
if err != nil {
if os.IsNotExist(err) {
return errors.Wrapf(err, "the manifest file %q does not exist", manifestPath)
}
return errors.Wrapf(err, "error obtaining stats for manifest file %q", manifestPath)
}
if s.IsDir() {
return errors.Errorf("%q is a directory", manifestPath)
}
}
return nil
}
func runDiff(flags *diffFlags, args []string) error {
var err error
var cfg *kubeadmapi.InitConfiguration

View File

@ -124,3 +124,60 @@ func TestRunDiff(t *testing.T) {
})
}
}
func TestValidateManifests(t *testing.T) {
// Create valid manifest paths
apiServerManifest, err := createTestRunDiffFile([]byte{})
if err != nil {
t.Fatal(err)
}
defer os.Remove(apiServerManifest)
controllerManagerManifest, err := createTestRunDiffFile([]byte{})
if err != nil {
t.Fatal(err)
}
defer os.Remove(controllerManagerManifest)
schedulerManifest, err := createTestRunDiffFile([]byte{})
if err != nil {
t.Fatal(err)
}
defer os.Remove(schedulerManifest)
// Create a file path that does not exist
notExistFilePath := "./foobar123456"
testCases := []struct {
name string
args []string
expectedError bool
}{
{
name: "valid: valid manifest path",
args: []string{apiServerManifest, controllerManagerManifest, schedulerManifest},
expectedError: false,
},
{
name: "invalid: one is empty path",
args: []string{apiServerManifest, controllerManagerManifest, ""},
expectedError: true,
},
{
name: "invalid: manifest path is directory",
args: []string{"./"},
expectedError: true,
},
{
name: "invalid: manifest path does not exist",
args: []string{notExistFilePath},
expectedError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if err := validateManifestsPath(tc.args...); (err != nil) != tc.expectedError {
t.Fatalf("expected error: %v, saw: %v, error: %v", tc.expectedError, (err != nil), err)
}
})
}
}