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

Adding event support #6

Merged
merged 2 commits into from
Feb 23, 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
14 changes: 14 additions & 0 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"

api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
"github.com/arangodb/k8s-operator/pkg/generated/clientset/versioned"
Expand Down Expand Up @@ -76,6 +78,8 @@ type Deployment struct {

eventCh chan *deploymentEvent
stopCh chan struct{}

eventsCli corev1.EventInterface
}

// New creates a new Deployment from the given API object.
Expand All @@ -90,6 +94,7 @@ func New(config Config, deps Dependencies, apiObject *api.ArangoDeployment) (*De
deps: deps,
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),
stopCh: make(chan struct{}),
eventsCli: deps.KubeCli.Core().Events(apiObject.GetNamespace()),
}

go d.run()
Expand Down Expand Up @@ -191,6 +196,15 @@ func (d *Deployment) handleUpdateEvent(event *deploymentEvent) error {
return nil
}

// createEvent creates a given event.
// On error, the error is logged.
func (d *Deployment) createEvent(evt *v1.Event) {
_, err := d.eventsCli.Create(evt)
if err != nil {
d.deps.Log.Error().Err(err).Interface("event", *evt).Msg("Failed to record event")
}
}

// Update the status of the API object from the internal status
func (d *Deployment) updateCRStatus() error {
if reflect.DeepEqual(d.apiObject.Status, d.status) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/deployment/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error {
if m.State != api.MemberStateNone {
continue
}
// Create pod
role := group.AsRole()
if group.IsArangod() {
args := d.createArangodArgs(apiObject, group, spec, d.status.Members.Agents, m.ID)
Expand All @@ -318,13 +319,16 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error {
return maskAny(err)
}
}
// Record new member state
m.State = api.MemberStateCreating
if err := status.Update(m); err != nil {
return maskAny(err)
}
if err := d.updateCRStatus(); err != nil {
return maskAny(err)
}
// Create event
d.createEvent(k8sutil.NewMemberAddEvent(m.PodName, role, apiObject))
}
return nil
}, &d.status); err != nil {
Expand Down
87 changes: 87 additions & 0 deletions pkg/util/k8sutil/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//

package k8sutil

import (
"fmt"
"os"
"strings"
"time"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/arangodb/k8s-operator/pkg/util/constants"
)

// APIObject helps to abstract an object from our custom API.
type APIObject interface {
metav1.Object
// AsOwner creates an OwnerReference for the given deployment
AsOwner() metav1.OwnerReference
}

// NewMemberAddEvent creates an event indicating that a member was added.
func NewMemberAddEvent(memberName, role string, apiObject APIObject) *v1.Event {
event := newDeploymentEvent(apiObject)
event.Type = v1.EventTypeNormal
event.Reason = fmt.Sprintf("New %s Added", strings.Title(role))
event.Message = fmt.Sprintf("New %s %s added to deployment", role, memberName)
return event
}

// MemberRemoveEvent creates an event indicating that an existing member was removed.
func MemberRemoveEvent(memberName, role string, apiObject APIObject) *v1.Event {
event := newDeploymentEvent(apiObject)
event.Type = v1.EventTypeNormal
event.Reason = fmt.Sprintf("%s Removed", strings.Title(role))
event.Message = fmt.Sprintf("Existing %s %s removed from the deployment", role, memberName)
return event
}

// newDeploymentEvent creates a new event for the given api object & owner.
func newDeploymentEvent(apiObject APIObject) *v1.Event {
t := time.Now()
owner := apiObject.AsOwner()
return &v1.Event{
ObjectMeta: metav1.ObjectMeta{
GenerateName: apiObject.GetName() + "-",
Namespace: apiObject.GetNamespace(),
},
InvolvedObject: v1.ObjectReference{
APIVersion: owner.APIVersion,
Kind: owner.Kind,
Name: owner.Name,
Namespace: apiObject.GetNamespace(),
UID: owner.UID,
ResourceVersion: apiObject.GetResourceVersion(),
},
Source: v1.EventSource{
Component: os.Getenv(constants.EnvOperatorPodName),
},
// Each deployment event is unique so it should not be collapsed with other events
FirstTimestamp: metav1.Time{Time: t},
LastTimestamp: metav1.Time{Time: t},
Count: int32(1),
}
}