Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/sdk: combining all pkgs under pkg/sdk into a single pkg #242

Merged
merged 1 commit into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Changed

- All the modules in [`pkg/sdk`](https://github.com/operator-framework/operator-sdk/tree/master/pkg/sdk) have been combined into a single package. `action`, `handler`, `informer` `types` and `query` pkgs have been consolidated into `pkg/sdk`. [#242](https://github.com/operator-framework/operator-sdk/pull/242)

### Removed

### Fixed
Expand Down
19 changes: 8 additions & 11 deletions example/memcached-operator/handler.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ import (

v1alpha1 "github.com/example-inc/memcached-operator/pkg/apis/cache/v1alpha1"

"github.com/operator-framework/operator-sdk/pkg/sdk/action"
"github.com/operator-framework/operator-sdk/pkg/sdk/handler"
"github.com/operator-framework/operator-sdk/pkg/sdk/query"
"github.com/operator-framework/operator-sdk/pkg/sdk/types"
"github.com/operator-framework/operator-sdk/pkg/sdk"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

func NewHandler() handler.Handler {
func NewHandler() sdk.Handler {
return &Handler{}
}

type Handler struct {
}

func (h *Handler) Handle(ctx types.Context, event types.Event) error {
func (h *Handler) Handle(ctx sdk.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *v1alpha1.Memcached:
memcached := o
Expand All @@ -37,20 +34,20 @@ func (h *Handler) Handle(ctx types.Context, event types.Event) error {

// Create the deployment if it doesn't exist
dep := deploymentForMemcached(memcached)
err := action.Create(dep)
err := sdk.Create(dep)
if err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create deployment: %v", err)
}

// Ensure the deployment size is the same as the spec
err = query.Get(dep)
err = sdk.Get(dep)
if err != nil {
return fmt.Errorf("failed to get deployment: %v", err)
}
size := memcached.Spec.Size
if *dep.Spec.Replicas != size {
dep.Spec.Replicas = &size
err = action.Update(dep)
err = sdk.Update(dep)
if err != nil {
return fmt.Errorf("failed to update deployment: %v", err)
}
Expand All @@ -60,14 +57,14 @@ func (h *Handler) Handle(ctx types.Context, event types.Event) error {
podList := podList()
labelSelector := labels.SelectorFromSet(labelsForMemcached(memcached.Name)).String()
listOps := &metav1.ListOptions{LabelSelector: labelSelector}
err = query.List(memcached.Namespace, podList, query.WithListOptions(listOps))
err = sdk.List(memcached.Namespace, podList, sdk.WithListOptions(listOps))
if err != nil {
return fmt.Errorf("failed to list pods: %v", err)
}
podNames := getPodNames(podList.Items)
if !reflect.DeepEqual(podNames, memcached.Status.Nodes) {
memcached.Status.Nodes = podNames
err := action.Update(memcached)
err := sdk.Update(memcached)
if err != nil {
return fmt.Errorf("failed to update memcached status: %v", err)
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,26 @@ const handlerExp = `package stub
import (
"github.com/example-inc/app-operator/pkg/apis/app/v1alpha1"

"github.com/operator-framework/operator-sdk/pkg/sdk/action"
"github.com/operator-framework/operator-sdk/pkg/sdk/handler"
"github.com/operator-framework/operator-sdk/pkg/sdk/types"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func NewHandler() handler.Handler {
func NewHandler() sdk.Handler {
return &Handler{}
}

type Handler struct {
// Fill me
}

func (h *Handler) Handle(ctx types.Context, event types.Event) error {
func (h *Handler) Handle(ctx sdk.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *v1alpha1.AppService:
err := action.Create(newbusyBoxPod(o))
err := sdk.Create(newbusyBoxPod(o))
if err != nil && !errors.IsAlreadyExists(err) {
logrus.Errorf("Failed to create busybox pod : %v", err)
return err
Expand Down
10 changes: 4 additions & 6 deletions pkg/generator/handler_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@ const handlerTmpl = `package stub
import (
"{{.RepoPath}}/pkg/apis/{{.APIDirName}}/{{.Version}}"

"{{.OperatorSDKImport}}/action"
"{{.OperatorSDKImport}}/handler"
"{{.OperatorSDKImport}}/types"
"{{.OperatorSDKImport}}"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func NewHandler() handler.Handler {
func NewHandler() sdk.Handler {
return &Handler{}
}

type Handler struct {
// Fill me
}

func (h *Handler) Handle(ctx types.Context, event types.Event) error {
func (h *Handler) Handle(ctx sdk.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *{{.Version}}.{{.Kind}}:
err := action.Create(newbusyBoxPod(o))
err := sdk.Create(newbusyBoxPod(o))
if err != nil && !errors.IsAlreadyExists(err) {
logrus.Errorf("Failed to create busybox pod : %v", err)
return err
Expand Down
43 changes: 38 additions & 5 deletions pkg/sdk/action/action.go → pkg/sdk/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package action
package sdk

import (
"fmt"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
sdkTypes "github.com/operator-framework/operator-sdk/pkg/sdk/types"
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Create creates the provided object on the server and updates the arg
// "object" with the result from the server(UID, resourceVersion, etc).
// Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name/GenerateName, Namespace) is missing or incorrect.
// Can also return an api error from the server
// e.g AlreadyExists https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L423
func Create(object sdkTypes.Object) (err error) {
func Create(object Object) (err error) {
_, namespace, err := k8sutil.GetNameAndNamespace(object)
if err != nil {
return err
Expand Down Expand Up @@ -59,7 +59,7 @@ func Create(object sdkTypes.Object) (err error) {
// Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name, Namespace) is missing or incorrect.
// Can also return an api error from the server
// e.g Conflict https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L428
func Update(object sdkTypes.Object) (err error) {
func Update(object Object) (err error) {
_, namespace, err := k8sutil.GetNameAndNamespace(object)
if err != nil {
return err
Expand All @@ -86,12 +86,45 @@ func Update(object sdkTypes.Object) (err error) {
return nil
}

// DeleteOp wraps all the options for Delete().
type DeleteOp struct {
metaDeleteOptions *metav1.DeleteOptions
}

// DeleteOption configures DeleteOp.
type DeleteOption func(*DeleteOp)

func NewDeleteOp() *DeleteOp {
op := &DeleteOp{}
op.setDefaults()
return op
}

func (op *DeleteOp) applyOpts(opts []DeleteOption) {
for _, opt := range opts {
opt(op)
}
}

func (op *DeleteOp) setDefaults() {
if op.metaDeleteOptions == nil {
op.metaDeleteOptions = &metav1.DeleteOptions{}
}
}

// WithDeleteOptions sets the metav1.DeleteOptions for the Delete() operation.
func WithDeleteOptions(metaDeleteOptions *metav1.DeleteOptions) DeleteOption {
return func(op *DeleteOp) {
op.metaDeleteOptions = metaDeleteOptions
}
}

// Delete deletes the specified object
// Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name, Namespace) is missing or incorrect.
// e.g NotFound https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L418
// “opts” configures the DeleteOptions
// When passed WithDeleteOptions(o), the specified metav1.DeleteOptions are set.
func Delete(object sdkTypes.Object, opts ...DeleteOption) (err error) {
func Delete(object Object, opts ...DeleteOption) (err error) {
name, namespace, err := k8sutil.GetNameAndNamespace(object)
if err != nil {
return err
Expand Down
16 changes: 0 additions & 16 deletions pkg/sdk/action/doc.go

This file was deleted.

52 changes: 0 additions & 52 deletions pkg/sdk/action/op.go

This file was deleted.

10 changes: 4 additions & 6 deletions pkg/sdk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import (
"context"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
sdkHandler "github.com/operator-framework/operator-sdk/pkg/sdk/handler"
sdkInformer "github.com/operator-framework/operator-sdk/pkg/sdk/informer"

"github.com/sirupsen/logrus"
)

var (
// informers is the set of all informers for the resources watched by the user
informers []sdkInformer.Informer
informers []Informer
)

// Watch watches for changes on the given resource.
Expand All @@ -47,14 +45,14 @@ func Watch(apiVersion, kind, namespace string, resyncPeriod int) {
logrus.Errorf("failed to get resource client for (apiVersion:%s, kind:%s, ns:%s): %v", apiVersion, kind, namespace, err)
panic(err)
}
informer := sdkInformer.New(resourcePluralName, namespace, resourceClient, resyncPeriod)
informer := NewInformer(resourcePluralName, namespace, resourceClient, resyncPeriod)
informers = append(informers, informer)
}

// Handle registers the handler for all events.
// In the future, we would have a mux-pattern to dispatch events to matched handlers.
func Handle(handler sdkHandler.Handler) {
sdkHandler.RegisteredHandler = handler
func Handle(handler Handler) {
RegisteredHandler = handler
}

// Run starts the process of Watching resources, handling Events, and processing Actions
Expand Down
8 changes: 2 additions & 6 deletions pkg/sdk/handler/handler.go → pkg/sdk/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package handler

import (
sdkTypes "github.com/operator-framework/operator-sdk/pkg/sdk/types"
)
package sdk

// Handler reacts to events and outputs actions.
// If any intended action failed, the event would be re-triggered.
// For actions done before the failed action, there is no rollback.
type Handler interface {
Handle(sdkTypes.Context, sdkTypes.Event) error
Handle(Context, Event) error
}

var (
Expand Down
10 changes: 4 additions & 6 deletions pkg/sdk/informer/sync.go → pkg/sdk/informer-sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package informer
package sdk

import (
sdkHandler "github.com/operator-framework/operator-sdk/pkg/sdk/handler"
sdkTypes "github.com/operator-framework/operator-sdk/pkg/sdk/types"
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -77,14 +75,14 @@ func (i *informer) sync(key string) error {
unstructObj := obj.(*unstructured.Unstructured).DeepCopy()
object := k8sutil.RuntimeObjectFromUnstructured(unstructObj)

event := sdkTypes.Event{
event := Event{
Object: object,
Deleted: !exists,
}

sdkCtx := sdkTypes.Context{Context: i.context}
sdkCtx := Context{Context: i.context}
// TODO: Add option to prevent multiple informers from invoking Handle() concurrently?
err = sdkHandler.RegisteredHandler.Handle(sdkCtx, event)
err = RegisteredHandler.Handle(sdkCtx, event)
if !exists && err == nil {
delete(i.deletedObjects, key)
}
Expand Down
Loading