Skip to content

Commit

Permalink
Merge pull request #1774 from saschagrunert/log-file
Browse files Browse the repository at this point in the history
Add log file support to krel stage/release
  • Loading branch information
k8s-ci-robot authored Nov 23, 2020
2 parents bec02d5 + 3572939 commit a76760c
Show file tree
Hide file tree
Showing 12 changed files with 581 additions and 30 deletions.
46 changes: 29 additions & 17 deletions pkg/anago/anago.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,30 @@ func (o *Options) String() string {
}

// Validate if the options are correctly set.
func (o *Options) Validate() (*State, error) {
func (o *Options) Validate(state *State) error {
logrus.Infof("Validating generic options: %s", o.String())
state := DefaultState()

if o.ReleaseType != release.ReleaseTypeAlpha &&
o.ReleaseType != release.ReleaseTypeBeta &&
o.ReleaseType != release.ReleaseTypeRC &&
o.ReleaseType != release.ReleaseTypeOfficial {
return nil, errors.Errorf("invalid release type: %s", o.ReleaseType)
return errors.Errorf("invalid release type: %s", o.ReleaseType)
}

if !git.IsReleaseBranch(o.ReleaseBranch) {
return nil, errors.Errorf("invalid release branch: %s", o.ReleaseBranch)
return errors.Errorf("invalid release branch: %s", o.ReleaseBranch)
}

semverBuildVersion, err := util.TagStringToSemver(o.BuildVersion)
if err != nil {
return nil, errors.Wrapf(err, "invalid build version: %s", o.BuildVersion)
return errors.Wrapf(err, "invalid build version: %s", o.BuildVersion)
}
if len(semverBuildVersion.Build) == 0 {
return nil, errors.Errorf("build version does not contain build commit")
return errors.Errorf("build version does not contain build commit")
}
state.semverBuildVersion = semverBuildVersion

return state, nil
return nil
}

// Bucket returns the Google Cloud Bucket for these `Options`.
Expand All @@ -125,6 +124,9 @@ func (o *Options) ContainerRegistry() string {
// State holds all inferred and calculated values from the release process
// it's state mutates as each step es executed
type State struct {
// logFile is the internal logging file target.
logFile string // nolint: structcheck

// semverBuildVersion is the parsed build version which is set after the
// validation.
semverBuildVersion semver.Version
Expand Down Expand Up @@ -181,12 +183,11 @@ func (s *StageOptions) String() string {
}

// Validate if the options are correctly set.
func (s *StageOptions) Validate() (*StageState, error) {
state, err := s.Options.Validate()
if err != nil {
return nil, errors.Wrap(err, "validating generic options")
func (s *StageOptions) Validate(state *State) error {
if err := s.Options.Validate(state); err != nil {
return errors.Wrap(err, "validating generic options")
}
return &StageState{State: state}, nil
return nil
}

// Stage is the structure to be used for staging releases.
Expand Down Expand Up @@ -217,6 +218,12 @@ func (s *Stage) Submit() error {
// staging bucket.
// nolint:dupl
func (s *Stage) Run() error {
s.client.InitState()

if err := s.client.InitLogFile(); err != nil {
return errors.Wrap(err, "init log file")
}

logger := log.NewStepLogger(9)
logger.Infof("Using krel version:\n%s", version.Get().String())

Expand Down Expand Up @@ -299,12 +306,11 @@ func (r *ReleaseOptions) String() string {
}

// Validate if the options are correctly set.
func (r *ReleaseOptions) Validate() (*ReleaseState, error) {
state, err := r.Options.Validate()
if err != nil {
return nil, errors.Wrap(err, "validating generic options")
func (r *ReleaseOptions) Validate(state *State) error {
if err := r.Options.Validate(state); err != nil {
return errors.Wrap(err, "validating generic options")
}
return &ReleaseState{State: state}, nil
return nil
}

// Release is the structure to be used for releasing staged releases.
Expand Down Expand Up @@ -334,6 +340,12 @@ func (r *Release) Submit() error {
// Run for for `Release` struct finishes a previously staged release.
// nolint:dupl
func (r *Release) Run() error {
r.client.InitState()

if err := r.client.InitLogFile(); err != nil {
return errors.Wrap(err, "init log file")
}

logger := log.NewStepLogger(9)
logger.Infof("Using krel version:\n%s", version.Get().String())

Expand Down
15 changes: 14 additions & 1 deletion pkg/anago/anago_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func TestRunStage(t *testing.T) {
},
shouldError: false,
},
{ // InitLogFile fails
prepare: func(mock *anagofakes.FakeStageClient) {
mock.InitLogFileReturns(err)
},
shouldError: true,
},
{ // ValidateOptions fails
prepare: func(mock *anagofakes.FakeStageClient) {
mock.ValidateOptionsReturns(err)
Expand Down Expand Up @@ -135,6 +141,12 @@ func TestRunRelease(t *testing.T) {
},
shouldError: false,
},
{ // InitLogFile fails
prepare: func(mock *anagofakes.FakeReleaseClient) {
mock.InitLogFileReturns(err)
},
shouldError: true,
},
{ // CheckPrerequisites fails
prepare: func(mock *anagofakes.FakeReleaseClient) {
mock.CheckPrerequisitesReturns(err)
Expand Down Expand Up @@ -245,7 +257,8 @@ func TestValidateOptions(t *testing.T) {
shouldError: true,
},
} {
_, err := tc.provided.Validate()
state := anago.DefaultState()
err := tc.provided.Validate(state)
if tc.shouldError {
require.NotNil(t, err)
} else {
Expand Down
95 changes: 95 additions & 0 deletions pkg/anago/anagofakes/fake_release_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions pkg/anago/anagofakes/fake_release_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a76760c

Please sign in to comment.