-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ExternalNode] Implement support bundle collection status on Controller
Signed-off-by: wenyingd <[email protected]>
- Loading branch information
Showing
11 changed files
with
1,292 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
pkg/apiserver/registry/controlplane/supportbundlecollection/subresources.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package supportbundlecollection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
|
||
"antrea.io/antrea/pkg/apis/controlplane" | ||
) | ||
|
||
// StatusREST implements the REST endpoint for getting NetworkPolicy's obj. | ||
type StatusREST struct { | ||
collector statusCollector | ||
} | ||
|
||
// NewStatusREST returns a REST object that will work against API services. | ||
func NewStatusREST(collector statusCollector) *StatusREST { | ||
return &StatusREST{collector} | ||
} | ||
|
||
// statusCollector is the interface required by the handler. | ||
type statusCollector interface { | ||
UpdateStatus(status *controlplane.SupportBundleCollectionStatus) error | ||
} | ||
|
||
var _ rest.NamedCreater = &StatusREST{} | ||
|
||
func (s StatusREST) New() runtime.Object { | ||
return &controlplane.SupportBundleCollectionStatus{} | ||
} | ||
|
||
func (s StatusREST) Create(ctx context.Context, name string, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) { | ||
status, ok := obj.(*controlplane.SupportBundleCollectionStatus) | ||
if !ok { | ||
return nil, errors.NewBadRequest(fmt.Sprintf("not a SupportBundleCollectionStatus object: %T", obj)) | ||
} | ||
if name != status.Name { | ||
return nil, errors.NewBadRequest("name in URL does not match name in SupportBundleCollectionStatus object") | ||
} | ||
err := s.collector.UpdateStatus(status) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &metav1.Status{Status: metav1.StatusSuccess}, nil | ||
} |
109 changes: 109 additions & 0 deletions
109
pkg/apiserver/registry/controlplane/supportbundlecollection/subresources_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package supportbundlecollection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"antrea.io/antrea/pkg/apis/controlplane" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
obj runtime.Object | ||
expError error | ||
}{ | ||
{ | ||
name: "invalid-status-type", | ||
obj: runtime.Object(&controlplane.SupportBundleCollection{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "invalid-status-type", | ||
}, | ||
}), | ||
expError: errors.NewBadRequest("not a SupportBundleCollectionStatus object"), | ||
}, | ||
{ | ||
name: "invalid-status-name", | ||
obj: runtime.Object(&controlplane.SupportBundleCollectionStatus{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "invalid-name", | ||
}, | ||
}), | ||
expError: errors.NewBadRequest("name in URL does not match name in SupportBundleCollectionStatus object"), | ||
}, | ||
{ | ||
name: "unable-update", | ||
obj: runtime.Object(&controlplane.SupportBundleCollectionStatus{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "unable-update", | ||
}, | ||
}), | ||
expError: fmt.Errorf("no Nodes status is updated"), | ||
}, | ||
{ | ||
name: "valid-status-update", | ||
obj: runtime.Object(&controlplane.SupportBundleCollectionStatus{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "valid-status-update", | ||
}, | ||
Nodes: []controlplane.SupportBundleCollectionNodeStatus{ | ||
{ | ||
NodeName: "n1", | ||
NodeType: "Node", | ||
}, | ||
}, | ||
}), | ||
expError: nil, | ||
}, | ||
} { | ||
statusController := &fakeStatusCollector{ | ||
bundleCollectionStatuses: make(map[string]*controlplane.SupportBundleCollectionStatus), | ||
} | ||
r := NewStatusREST(statusController) | ||
rsp, err := r.Create(context.TODO(), tc.name, tc.obj, nil, &metav1.CreateOptions{}) | ||
if tc.expError == nil { | ||
assert.NoError(t, err) | ||
assert.Equal(t, &metav1.Status{Status: metav1.StatusSuccess}, rsp) | ||
expStatus := tc.obj.(*controlplane.SupportBundleCollectionStatus) | ||
status := statusController.bundleCollectionStatuses[tc.name] | ||
assert.Equal(t, expStatus, status) | ||
} else { | ||
assert.Error(t, err) | ||
assert.True(t, strings.Contains(err.Error(), tc.expError.Error())) | ||
} | ||
} | ||
} | ||
|
||
type fakeStatusCollector struct { | ||
bundleCollectionStatuses map[string]*controlplane.SupportBundleCollectionStatus | ||
} | ||
|
||
func (c *fakeStatusCollector) UpdateStatus(status *controlplane.SupportBundleCollectionStatus) error { | ||
bundleCollectionName := status.Name | ||
if len(status.Nodes) == 0 { | ||
return fmt.Errorf("no Nodes status is updated") | ||
} | ||
c.bundleCollectionStatuses[bundleCollectionName] = status | ||
return nil | ||
} |
25 changes: 25 additions & 0 deletions
25
...ntset/versioned/typed/controlplane/v1beta2/fake/fake_supportbundlecollection_expansion.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
|
||
"antrea.io/antrea/pkg/apis/controlplane/v1beta2" | ||
) | ||
|
||
func (c *FakeSupportBundleCollections) UpdateStatus(ctx context.Context, name string, status *v1beta2.SupportBundleCollectionStatus) error { | ||
return nil | ||
} |
2 changes: 0 additions & 2 deletions
2
pkg/client/clientset/versioned/typed/controlplane/v1beta2/generated_expansion.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...lient/clientset/versioned/typed/controlplane/v1beta2/supportbundlecollection_expansion.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v1beta2 | ||
|
||
import ( | ||
"context" | ||
|
||
"antrea.io/antrea/pkg/apis/controlplane/v1beta2" | ||
) | ||
|
||
type SupportBundleCollectionExpansion interface { | ||
UpdateStatus(ctx context.Context, name string, status *v1beta2.SupportBundleCollectionStatus) error | ||
} | ||
|
||
func (c *supportBundleCollections) UpdateStatus(ctx context.Context, name string, status *v1beta2.SupportBundleCollectionStatus) error { | ||
return c.client.Post().Resource("supportbundlecollections").Name(name).SubResource("status").Body(status).Do(ctx).Error() | ||
} |
Oops, something went wrong.