Skip to content

Commit

Permalink
pkg/*: Create new client for Service get/create
Browse files Browse the repository at this point in the history
Due to the cache not being started at the time when we attempt to query
for the Service, we instead create a new client in a similiar way as we
do with leader elections.
  • Loading branch information
lilic committed Jan 24, 2019
1 parent 68452c3 commit 84d2432
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
29 changes: 19 additions & 10 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/manager"
"k8s.io/client-go/rest"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

Expand All @@ -35,7 +36,7 @@ var log = logf.Log.WithName("metrics")
const PrometheusPortName = "metrics"

// ExposeMetricsPort creates a Kubernetes Service to expose the passed metrics port.
func ExposeMetricsPort(port int32, mgr manager.Manager) (*v1.Service, error) {
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error) {
// We do not need to check the validity of the port, as controller-runtime
// would error out and we would never get to this stage.
s, err := initOperatorService(port, PrometheusPortName)
Expand All @@ -46,32 +47,40 @@ func ExposeMetricsPort(port int32, mgr manager.Manager) (*v1.Service, error) {
}
return nil, fmt.Errorf("failed to initialize service object for metrics: %v", err)
}
service, err := createService(mgr, s)
service, err := createService(ctx, s)
if err != nil {
return nil, fmt.Errorf("failed to create or get service for metrics: %v", err)
}

return service, nil
}

func createService(mgr manager.Manager, s *v1.Service) (*v1.Service, error) {
client := mgr.GetClient()
if err := client.Create(context.TODO(), s); err != nil {
func createService(ctx context.Context, s *v1.Service) (*v1.Service, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}

client, err := crclient.New(config, crclient.Options{})
if err != nil {
return nil, err
}

if err := client.Create(ctx, s); err != nil {
if !apierrors.IsAlreadyExists(err) {
return nil, err
}
// Get existing Service and return it
existingService := &v1.Service{}
err := client.Get(context.TODO(), types.NamespacedName{
Name: s.ObjectMeta.Name,
Namespace: s.ObjectMeta.Namespace,
err := client.Get(ctx, types.NamespacedName{
Name: s.Name,
Namespace: s.Namespace,
}, existingService)
if err != nil {
return nil, err
}
log.Info("Metrics Service object already exists", "name", existingService.Name)
return existingService, nil

}

log.Info("Metrics Service object created", "name", s.Name)
Expand Down
6 changes: 4 additions & 2 deletions pkg/scaffold/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ func main() {
log.Error(err, "")
os.Exit(1)
}
ctx := context.TODO()
// Become the leader before proceeding
err = leader.Become(context.TODO(), "{{ .ProjectName }}-lock")
err = leader.Become(ctx, "{{ .ProjectName }}-lock")
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand Down Expand Up @@ -126,7 +128,7 @@ func main() {
}
// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(metricsPort, mgr)
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
log.Info(err.Error())
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/scaffold/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ func main() {
os.Exit(1)
}
ctx := context.TODO()
// Become the leader before proceeding
err = leader.Become(context.TODO(), "app-operator-lock")
err = leader.Become(ctx, "app-operator-lock")
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand Down Expand Up @@ -124,7 +126,7 @@ func main() {
}
// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(metricsPort, mgr)
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
log.Info(err.Error())
}
Expand Down

0 comments on commit 84d2432

Please sign in to comment.