Skip to content

Commit

Permalink
handle error that occurs if a team has no state
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Feb 16, 2024
1 parent ac67206 commit 04c689d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
4 changes: 3 additions & 1 deletion internal/reconcilers/dependencytrack/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/nais/dependencytrack/pkg/client"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestMissingConfig(t *testing.T) {
Expand Down Expand Up @@ -64,7 +66,7 @@ func TestDependencytrackReconciler_Reconcile(t *testing.T) {
apiClient, grpcServers := apiclient.NewMockClient(t)
grpcServers.Reconcilers.EXPECT().
State(mock.Anything, &protoapi.GetReconcilerStateRequest{ReconcilerName: "nais:dependencytrack", TeamSlug: teamSlug}).
Return(&protoapi.GetReconcilerStateResponse{}, nil).
Return(nil, status.Error(codes.NotFound, "state not found")).
Once()
grpcServers.Teams.EXPECT().
Members(mock.Anything, &protoapi.ListTeamMembersRequest{Slug: teamSlug, Limit: 100, Offset: 0}).
Expand Down
15 changes: 9 additions & 6 deletions internal/reconcilers/dependencytrack/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/nais/api/pkg/apiclient"
"github.com/nais/api/pkg/protoapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type DependencyTrackState struct {
Expand All @@ -29,19 +31,20 @@ func (r *reconciler) saveState(ctx context.Context, client *apiclient.APIClient,
}

func (r *reconciler) loadState(ctx context.Context, client *apiclient.APIClient, teamSlug string) (*DependencyTrackState, error) {
st := DependencyTrackState{}
resp, err := client.Reconcilers().State(ctx, &protoapi.GetReconcilerStateRequest{
ReconcilerName: reconcilerName,
TeamSlug: teamSlug,
})
if err != nil {
if e, ok := status.FromError(err); ok && e.Code() == codes.NotFound {
// special case: team does not yet have any state
return &st, nil
}
return nil, err
}

st := DependencyTrackState{}
if resp.State == nil {
} else if resp.State == nil {
return &st, nil
}
if err := json.Unmarshal(resp.State.Value, &st); err != nil {
} else if err := json.Unmarshal(resp.State.Value, &st); err != nil {
return nil, err
}
return &st, nil
Expand Down
4 changes: 3 additions & 1 deletion internal/reconcilers/github/team/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/shurcooL/githubv4"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -47,7 +49,7 @@ func TestGitHubReconciler_getOrCreateTeam(t *testing.T) {
Once()
mockServer.Reconcilers.EXPECT().
State(mock.Anything, &protoapi.GetReconcilerStateRequest{ReconcilerName: "github:team", TeamSlug: teamSlug}).
Return(&protoapi.GetReconcilerStateResponse{}, nil).
Return(nil, status.Error(codes.NotFound, "state not found")).
Once()
mockServer.Teams.EXPECT().
SetTeamExternalReferences(mock.Anything, mock.MatchedBy(func(req *protoapi.SetTeamExternalReferencesRequest) bool {
Expand Down
16 changes: 9 additions & 7 deletions internal/reconcilers/github/team/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/nais/api/pkg/apiclient"
"github.com/nais/api/pkg/protoapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type GitHubState struct {
Expand Down Expand Up @@ -66,20 +68,20 @@ func GetTeamRepositories(ctx context.Context, client protoapi.ReconcilersClient,
}

func getState(ctx context.Context, client protoapi.ReconcilersClient, teamSlug string) (*GitHubState, error) {
st := GitHubState{}
resp, err := client.State(ctx, &protoapi.GetReconcilerStateRequest{
ReconcilerName: reconcilerName,
TeamSlug: teamSlug,
})
if err != nil {
if e, ok := status.FromError(err); ok && e.Code() == codes.NotFound {
// special case: team does not yet have any state
return &st, nil
}
return nil, err
}

st := GitHubState{}
if resp.State == nil {
} else if resp.State == nil {
return &st, nil
}

if err := json.Unmarshal(resp.State.Value, &st); err != nil {
} else if err := json.Unmarshal(resp.State.Value, &st); err != nil {
return nil, err
}
return &st, nil
Expand Down
4 changes: 3 additions & 1 deletion internal/reconcilers/nais/namespace/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"github.com/stretchr/testify/mock"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -84,7 +86,7 @@ func TestReconcile(t *testing.T) {
apiClient, mockServer := apiclient.NewMockClient(t)
mockServer.Reconcilers.EXPECT().
State(mock.Anything, &protoapi.GetReconcilerStateRequest{TeamSlug: teamSlug, ReconcilerName: "nais:namespace"}).
Return(&protoapi.GetReconcilerStateResponse{}, nil).
Return(nil, status.Error(codes.NotFound, "state not found")).
Once()
mockServer.Reconcilers.EXPECT().
SaveState(mock.Anything, &protoapi.SaveReconcilerStateRequest{Value: []byte("{}"), TeamSlug: teamSlug, ReconcilerName: "nais:namespace"}).
Expand Down
15 changes: 9 additions & 6 deletions internal/reconcilers/nais/namespace/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/nais/api/pkg/apiclient"
"github.com/nais/api/pkg/protoapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// state is a map of namespace names to unix timestamps
Expand All @@ -26,19 +28,20 @@ func (r *naisNamespaceReconciler) saveState(ctx context.Context, client *apiclie
}

func (r *naisNamespaceReconciler) loadState(ctx context.Context, client *apiclient.APIClient, teamSlug string) (state, error) {
st := state{}
resp, err := client.Reconcilers().State(ctx, &protoapi.GetReconcilerStateRequest{
ReconcilerName: r.Name(),
TeamSlug: teamSlug,
})
if err != nil {
if e, ok := status.FromError(err); ok && e.Code() == codes.NotFound {
// special case: team does not yet have any state
return st, nil
}
return nil, err
}

st := state{}
if resp.State == nil {
} else if resp.State == nil {
return st, nil
}
if err := json.Unmarshal(resp.State.Value, &st); err != nil {
} else if err := json.Unmarshal(resp.State.Value, &st); err != nil {
return nil, err
}
return st, nil
Expand Down

0 comments on commit 04c689d

Please sign in to comment.