Skip to content

Commit 2b6c0ff

Browse files
authored
Merge pull request #131 from hasbro17/haseeb/action-Create
action: add Create
2 parents e2cafaf + 2663eab commit 2b6c0ff

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

pkg/sdk/action/api.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package action
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/coreos/operator-sdk/pkg/k8sclient"
21+
sdkTypes "github.com/coreos/operator-sdk/pkg/sdk/types"
22+
"github.com/coreos/operator-sdk/pkg/util/k8sutil"
23+
)
24+
25+
// Create creates the provided object on the server and updates the arg
26+
// "object" with the result from the server(UID, resourceVersion, etc).
27+
// Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name/GenerateName, Namespace) is missing or incorrect.
28+
// Can also return an api error from the server
29+
// e.g AlreadyExists https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L423
30+
func Create(object sdkTypes.Object) (err error) {
31+
_, namespace, err := k8sutil.GetNameAndNamespace(object)
32+
if err != nil {
33+
return err
34+
}
35+
gvk := object.GetObjectKind().GroupVersionKind()
36+
apiVersion, kind := gvk.ToAPIVersionAndKind()
37+
38+
resourceClient, _, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
39+
if err != nil {
40+
return fmt.Errorf("failed to get resource client: %v", err)
41+
}
42+
43+
unstructObj := k8sutil.UnstructuredFromRuntimeObject(object)
44+
unstructObj, err = resourceClient.Create(unstructObj)
45+
if err != nil {
46+
return err
47+
}
48+
49+
// Update the arg object with the result
50+
err = k8sutil.UnstructuredIntoRuntimeObject(unstructObj, object)
51+
if err != nil {
52+
return fmt.Errorf("failed to unmarshal the retrieved data: %v", err)
53+
}
54+
return nil
55+
}

pkg/sdk/action/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package action contains a set of APIs for mutating kubernetes objects.
16+
package action

0 commit comments

Comments
 (0)