Skip to content
This repository was archived by the owner on Sep 21, 2020. It is now read-only.

Wrong operator-sdk imports in handler.go #14

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The first step to leveraging the Operator Lifecycle Manager is to create a manif

For the purpose of this guide, we will continue with this [predefined manifest][manifest_v1] file for the next steps. If you’d like, you can alter the image field within this manifest to reflect the image you built in previous steps, but it is unnecessary. In the future, the Operator SDK CLI will generate an Operator manifest for you, a feature that is planned for the next release of the Operator SDK.

### Deploy the Operator
### Deploy the Operator

Deploying an Operator is as simple as applying the Operator’s manifest to the desired namespace in the cluster.

Expand All @@ -143,13 +143,13 @@ $ kubectl apply -f deploy/rbac.yaml
$ sed '/---/q' deploy/operator.yaml | kubectl apply -f -
```

Because the Operator Lifecycle Manager creates Operators in a particular namespace when a manifest has been applied, administrators can leverage the native Kubernetes RBAC permission model to restrict which users are allowed to install Operators.
Because the Operator Lifecycle Manager creates Operators in a particular namespace when a manifest has been applied, administrators can leverage the native Kubernetes RBAC permission model to restrict which users are allowed to install Operators.

### Create an application instance
### Create an application instance

The Memcached Operator is now running in the `memcached` namespace. Users interact with Operators via instances of CustomResources; in this case, the resource has the Kind `Memcached`. Native Kubernetes RBAC also applies to CustomResources, providing administrators control over who can interact with each Operator.
The Memcached Operator is now running in the `memcached` namespace. Users interact with Operators via instances of CustomResources; in this case, the resource has the Kind `Memcached`. Native Kubernetes RBAC also applies to CustomResources, providing administrators control over who can interact with each Operator.

Creating instances of Memcached in this namespace will now trigger the Memcached Operator to instantiate pods running the memcached server that are managed by the Operator. The more CustomResources you create, the more unique instances of Memcached will be managed by the Memcached Operator running in this namespace.
Creating instances of Memcached in this namespace will now trigger the Memcached Operator to instantiate pods running the memcached server that are managed by the Operator. The more CustomResources you create, the more unique instances of Memcached will be managed by the Memcached Operator running in this namespace.

```sh
$ cat <<EOF | kubectl apply -f -
Expand Down Expand Up @@ -207,7 +207,7 @@ Hopefully, this guide was an effective demonstration of the value of the Operato
[layout_doc]: https://github.com/operator-framework/operator-sdk/blob/master/doc/project_layout.md
[quay_io]: https://www.quay.io
[kubernetes_cr]: https://kubernetes.io/docs/concepts/api-extension/custom-resources/
[handler_go]: https://github.com/operator-framework/getting-started/blob/master/handler.go.tmpl#L7
[handler_go]: https://github.com/operator-framework/getting-started/blob/master/handler.go.tmpl#L8
[create_public_image]: https://quay.io/new/
[manifest_v1]: memcachedoperator.0.0.1.csv.yaml
[manifest_v2]: memcachedoperator.0.0.2.csv.yaml
Expand Down
22 changes: 10 additions & 12 deletions handler.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package stub

import (
"context"
"fmt"
"reflect"

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 context.Context, event sdk.Event) error {
switch o := event.Object.(type) {
case *v1alpha1.Memcached:
memcached := o
Expand All @@ -37,20 +35,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 +58,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 Expand Up @@ -157,4 +155,4 @@ func getPodNames(pods []v1.Pod) []string {
podNames = append(podNames, pod.Name)
}
return podNames
}
}