Skip to content
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
5 changes: 5 additions & 0 deletions pkg/generator/deploy_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ spec:
command:
- {{.ProjectName}}
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
`

const rbacYamlTmpl = `kind: Role
Expand Down
5 changes: 5 additions & 0 deletions pkg/generator/gen_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ spec:
command:
- app-operator
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
`

const rbacYamlExp = `kind: Role
Expand Down
3 changes: 3 additions & 0 deletions pkg/generator/gen_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
const (
// sdkImport is the operator-sdk import path.
sdkImport = "github.com/operator-framework/operator-sdk/pkg/sdk"
k8sutilImport = "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
versionImport = "github.com/operator-framework/operator-sdk/version"
)

Expand All @@ -32,6 +33,7 @@ type Main struct {
// imports
OperatorSDKImport string
StubImport string
K8sutilImport string
SDKVersionImport string

APIVersion string
Expand All @@ -49,6 +51,7 @@ func renderMainFile(w io.Writer, repo, apiVersion, kind string) error {
m := Main{
OperatorSDKImport: sdkImport,
StubImport: filepath.Join(repo, stubDir),
K8sutilImport: k8sutilImport,
SDKVersionImport: versionImport,
APIVersion: apiVersion,
Kind: kind,
Expand Down
18 changes: 14 additions & 4 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

stub "github.com/example-inc/app-operator/pkg/stub"
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
sdkVersion "github.com/operator-framework/operator-sdk/version"

"github.com/sirupsen/logrus"
Expand All @@ -40,7 +41,16 @@ func printVersion() {

func main() {
printVersion()
sdk.Watch("app.example.com/v1alpha1", "AppService", "default", 5)

resource := "app.example.com/v1alpha1"
kind := "AppService"
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
logrus.Fatalf("Failed to get watch namespace: %v", err)
}
resyncPeriod := 5
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Handle(stub.NewHandler())
sdk.Run(context.TODO())
}
Expand All @@ -54,7 +64,7 @@ func TestGenMain(t *testing.T) {
}

if mainExp != buf.String() {
t.Errorf("want %v, got %v", mainExp, buf.String())
t.Errorf("want %v\ngot %v", mainExp, buf.String())
}
}

Expand Down Expand Up @@ -104,8 +114,8 @@ func newbusyBoxPod(cr *v1alpha1.AppService) *v1.Pod {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "busy-box",
Namespace: "default",
Name: "busy-box",
Namespace: cr.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cr, schema.GroupVersionKind{
Group: v1alpha1.SchemeGroupVersion.Group,
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/handler_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func newbusyBoxPod(cr *{{.Version}}.{{.Kind}}) *v1.Pod {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "busy-box",
Namespace: "default",
Name: "busy-box",
Namespace: cr.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cr, schema.GroupVersionKind{
Group: {{.Version}}.SchemeGroupVersion.Group,
Expand Down
12 changes: 11 additions & 1 deletion pkg/generator/main_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

stub "{{.StubImport}}"
sdk "{{.OperatorSDKImport}}"
k8sutil "{{.K8sutilImport}}"
sdkVersion "{{.SDKVersionImport}}"

"github.com/sirupsen/logrus"
Expand All @@ -36,7 +37,16 @@ func printVersion() {

func main() {
printVersion()
sdk.Watch("{{.APIVersion}}", "{{.Kind}}", "default", 5)

resource := "{{.APIVersion}}"
kind := "{{.Kind}}"
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
logrus.Fatalf("Failed to get watch namespace: %v", err)
}
resyncPeriod := 5
logrus.Infof("Watching %s, %s, %s, %d", resource, kind, namespace, resyncPeriod)
sdk.Watch(resource, kind, namespace, resyncPeriod)
sdk.Handle(stub.NewHandler())
sdk.Run(context.TODO())
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/k8sutil/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ const (
// KubeConfigEnvVar defines the env variable KUBERNETES_CONFIG which
// contains the kubeconfig file path.
KubeConfigEnvVar = "KUBERNETES_CONFIG"

// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which is the namespace that the pod is currently running in.
WatchNamespaceEnvVar = "WATCH_NAMESPACE"
)
13 changes: 13 additions & 0 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package k8sutil
import (
"encoding/json"
"fmt"
"os"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -134,3 +135,15 @@ func GetNameAndNamespace(object runtime.Object) (string, string, error) {
func ObjectInfo(kind, name, namespace string) string {
return kind + ": " + namespace + "/" + name
}

// GetWatchNamespace returns the namespace the operator should be watching for changes
func GetWatchNamespace() (string, error) {
ns, found := os.LookupEnv(WatchNamespaceEnvVar)
if !found {
return "", fmt.Errorf("%s must be set", WatchNamespaceEnvVar)
}
if len(ns) == 0 {
return "", fmt.Errorf("%s must not be empty", WatchNamespaceEnvVar)
}
return ns, nil
}