Skip to content

Commit

Permalink
Fix rare case of broken status (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gchbg authored May 14, 2024
1 parent 870c75b commit bd5431c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: ## Run tests.
@go run github.com/onsi/ginkgo/v2/ginkgo -r --race --randomize-suites --keep-going --randomize-all --repeat=1
@go run github.com/onsi/ginkgo/v2/ginkgo -r --race --randomize-suites --keep-going --randomize-all --repeat=11

.PHONY: lint
lint: ## Run golangci-lint linter & yamllint.
Expand Down
33 changes: 23 additions & 10 deletions internal/controller/oob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type access struct {
Protocol metalv1alpha1.Protocol `yaml:"protocol"`
Flags map[string]string `yaml:"flags"`
DefaultCredentials []bmc.Credentials `yaml:"defaultCredentials"`
Type bmc.Typ `yaml:"type"`
Type metalv1alpha1.OOBType `yaml:"type"`
}

type ctxkOOBHost struct{}
Expand Down Expand Up @@ -709,7 +709,7 @@ func (r *OOBReconciler) processCredentials(ctx context.Context, oob *metalv1alph
}
}

if oob.Spec.Protocol == nil || (creds.Username == "" && creds.Password == "") {
if oob.Spec.Protocol == nil || oob.Status.Type == "" || (creds.Username == "" && creds.Password == "") {
a, ok := r.macDB.Get(oob.Spec.MACAddress)
if !ok {
return r.setError(ctx, oob, apply, status, OOBErrorBadCredentials, fmt.Errorf("cannot find MAC address in MAC DB: %s", oob.Spec.MACAddress))
Expand All @@ -734,7 +734,7 @@ func (r *OOBReconciler) processCredentials(ctx context.Context, oob *metalv1alph
oob.Spec.Protocol = &a.Protocol
oob.Spec.Flags = a.Flags
defaultCreds = a.DefaultCredentials
oob.Status.Type = metalv1alpha1.OOBType(a.Type)
oob.Status.Type = a.Type
log.Debug(ctx, "Setting protocol, flags, and type")
if apply == nil {
var err error
Expand All @@ -748,14 +748,12 @@ func (r *OOBReconciler) processCredentials(ctx context.Context, oob *metalv1alph
WithName(oob.Spec.Protocol.Name).
WithPort(oob.Spec.Protocol.Port)).
WithFlags(oob.Spec.Flags))
if a.Type != "" {
applyst, err := metalv1alpha1apply.ExtractOOBStatus(oob, OOBFieldManager)
if err != nil {
return ctx, nil, nil, fmt.Errorf("cannot extract OOB status: %w", err)
}
status = util.Ensure(applyst.Status).
WithType(metalv1alpha1.OOBType(a.Type))
applyst, err := metalv1alpha1apply.ExtractOOBStatus(oob, OOBFieldManager)
if err != nil {
return ctx, nil, nil, fmt.Errorf("cannot extract OOB status: %w", err)
}
status = util.Ensure(applyst.Status).
WithType(a.Type)
}

b, err := bmc.NewBMC(string(oob.Spec.Protocol.Name), oob.Spec.Flags, host, oob.Spec.Protocol.Port, creds, expiration)
Expand Down Expand Up @@ -1236,6 +1234,21 @@ func loadMacDB(dbFile string) (util.PrefixMap[access], error) {

db := make(util.PrefixMap[access], len(dbf.MACs))
for _, m := range dbf.MACs {
if m.access.Protocol.Name == "" {
return nil, fmt.Errorf("prefix %s has no protocol name", m.Prefix)
}
if len(m.access.DefaultCredentials) == 0 {
return nil, fmt.Errorf("prefix %s has no default credentials", m.Prefix)
}
for _, dc := range m.access.DefaultCredentials {
if dc.Username == "" && dc.Password == "" {
return nil, fmt.Errorf("prefix %s has invalid default credentials", m.Prefix)
}
}
if m.access.Type == "" {
return nil, fmt.Errorf("prefix %s has no type", m.Prefix)
}

db[m.Prefix] = m.access
}

Expand Down

0 comments on commit bd5431c

Please sign in to comment.