Skip to content

Commit

Permalink
Remove ProjectReference superfluous logs
Browse files Browse the repository at this point in the history
* Developers: Use the '--zap-level' flag as part of the `--operator-flags`
* SysAdmins : Add the `--zap-level` as `arg` of the operator.yaml
* Apart from the controllers, the GCP Operators uses the gcpclient as well.

Things done:
* Add ProjectReference v1 verbose level
* Add ProjectClaim v2 verbose level
* Add gcpclient v3 verbose level
* Add operator-sdk v4 verbose level
* Expose v1-v4 verboseLevels as a  constant to replace log numerals

Suppression notes:
* During deletion the [INFO] logs are still visible.
* Hide all the [INFO] logs of ProjectClaim under the V1 level of verbosity
* Hide the the [INFO] type logs of gcpclient into V3 level of verbosity.
* Hide the operator-sdk initialization logs under v4 level.

refactors:
* Change all commands to shell
* conform to commitlint changes
  • Loading branch information
drpaneas authored and georgettica committed Jun 3, 2020
1 parent fdaba24 commit dcb3ff8
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 72 deletions.
15 changes: 8 additions & 7 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/openshift/gcp-project-operator/pkg/apis"
"github.com/openshift/gcp-project-operator/pkg/controller"
logtypes "github.com/openshift/gcp-project-operator/pkg/util/types"

"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
Expand All @@ -32,9 +33,9 @@ var (
var log = logf.Log.WithName("cmd")

func printVersion() {
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
log.V(logtypes.OperatorSDK).Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
log.V(logtypes.OperatorSDK).Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
log.V(logtypes.OperatorSDK).Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
}

func main() {
Expand Down Expand Up @@ -95,7 +96,7 @@ func run() error {
return err
}

log.Info("Registering Components.")
log.V(logtypes.OperatorSDK).Info("Registering Components.")

// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
Expand All @@ -112,15 +113,15 @@ func run() error {
// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
log.Info(err.Error())
log.V(logtypes.OperatorSDK).Info(err.Error())
}

// start cache and wait for sync
log.Info("init chache")
log.V(logtypes.OperatorSDK).Info("init chache")
cache := mgr.GetCache()
go cache.Start(stopCh)
cache.WaitForCacheSync(stopCh)
log.Info("Starting the Cmd.")
log.V(logtypes.OperatorSDK).Info("Starting the Cmd.")

// Start the Cmd
return mgr.Start(stopCh)
Expand Down
26 changes: 26 additions & 0 deletions docs/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

Some useful commands:

### Log verbosity

You can modify the verbosity by adding `args:`to the [operator.yaml](../deploy/operator.yaml)

If you want to see `info` logs coming from the `ProjectReference` use `--zap-level=X`, where X can be:

1. ProjectReference logs
2. ProjectClaim logs
3. gcp client logs
4. operator-sdk logs (golang version, etc)

as described in the type [ComponentLogLevel](../pkg/util/types/comonentloglevel.go)

e.g. if you are interested only in error messages, you can do it like this:

```yaml
containers:
- name: gcp-project-operator
image: quay.io/app-sre/gcp-project-operator
command:
- gcp-project-operator
args:
- '--zap-level=error'
imagePullPolicy: Always
```
### ProjectClaim
The `ProjectClaim` is getting deployed onto a namespace defined at the Resource.
Expand Down
21 changes: 17 additions & 4 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The operator can run either:

No matter which option you choose, before running the Operator you have to create the following Custom Resource Definitions on the cluster:

```zsh
```shell
kubectl create -f deploy/crds/gcp_v1alpha1_projectclaim_crd.yaml
kubectl create -f deploy/crds/gcp_v1alpha1_projectreference_crd.yaml
```
Expand All @@ -46,20 +46,33 @@ kubectl create -f deploy/crds/gcp_v1alpha1_projectreference_crd.yaml

Make sure you have the [operator-sdk](https://github.com/operator-framework/operator-sdk/releases) binary in your `$PATH` and run it locally:

```zsh
```shell
$ operator-sdk run --local --namespace gcp-project-operator
```

You will see some initialization logs.
The Operator will remain _idle_ after that, waiting for `ProjectClaim` resources to be present in the cluster.

You can change the verbosity of the logs, by passing the `--zap-level` flag as part of the `--operator-flags`:

```shell
run --local --namespace gcp-project-operator --operator-flags --zap-level=99
```

* Level 1: ProjectReference
* Level 2: ProjectClaim
* Level 3: gcpclient
* Level 4: operator-sdk framrwork logs (golang version, etc)

all of the levels are saved in an package called [github.com/openshift/gcp-project-operator/pkg/util/types](../pkg/util/types/comonentlog)

### Remotely

#### Pushing Image a container registry

Push the image to your container registry of your choice. For example:

```bash
```shell
username="razevedo"
podman build . -f build/Dockerfile -t "quay.io/$username/gcp-project-operator"
podman push "quay.io/$username/gcp-project-operator"
Expand All @@ -85,7 +98,7 @@ kubectl scale deployment gcp-project-operator -n gcp-project-operator --replicas

Otherwise, you can directly upload the image to your kubernetes cluster by hand

```zsh
```shell
# Export the image locally
docker save $image-name > image-name.tar

Expand Down
1 change: 0 additions & 1 deletion pkg/controller/projectclaim/projectclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func NewReconcileProjectClaim(client client.Client, scheme *runtime.Scheme) *Rec
// Reconcile calls ReconcileHandler and updates the CRD if any err occurs
func (r *ReconcileProjectClaim) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling ProjectClaim")

// Fetch the ProjectClaim instance
instance := &gcpv1alpha1.ProjectClaim{}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/projectclaim/projectclaimadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/go-logr/logr"
"github.com/openshift/cluster-api/pkg/util"
logtypes "github.com/openshift/gcp-project-operator/pkg/util/types"

gcpv1alpha1 "github.com/openshift/gcp-project-operator/pkg/apis/gcp/v1alpha1"
condition "github.com/openshift/gcp-project-operator/pkg/condition"
operrors "github.com/openshift/gcp-project-operator/pkg/util/errors"
Expand Down Expand Up @@ -175,7 +177,7 @@ func (c *ProjectClaimAdapter) EnsureProjectReferenceLink() (ObjectState, error)

func (c *ProjectClaimAdapter) EnsureFinalizer() (ObjectState, error) {
if !util.Contains(c.projectClaim.GetFinalizers(), ProjectClaimFinalizer) {
c.logger.Info("Adding Finalizer to the ProjectClaim")
c.logger.V(int(logtypes.ProjectClaim)).Info("Adding Finalizer to the ProjectClaim")
c.projectClaim.SetFinalizers(append(c.projectClaim.GetFinalizers(), ProjectClaimFinalizer))

err := c.client.Update(context.TODO(), c.projectClaim)
Expand Down
28 changes: 14 additions & 14 deletions pkg/controller/projectreference/projectreference_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/openshift/gcp-project-operator/pkg/gcpclient"
gcputil "github.com/openshift/gcp-project-operator/pkg/util"
operrors "github.com/openshift/gcp-project-operator/pkg/util/errors"
logtypes "github.com/openshift/gcp-project-operator/pkg/util/types"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/googleapi"
"google.golang.org/api/iam/v1"
Expand Down Expand Up @@ -96,7 +97,6 @@ func (r *ReferenceAdapter) EnsureProjectClaimReady() (gcpv1alpha1.ClaimStatus, e
}

if r.ProjectReference.Status.State == gcpv1alpha1.ProjectReferenceStatusReady && r.ProjectClaim.Status.State == gcpv1alpha1.ClaimStatusReady {
r.logger.Info("ProjectReference and ProjectClaim CR are in READY state nothing to process.")
return r.ProjectClaim.Status.State, nil
}

Expand Down Expand Up @@ -148,21 +148,21 @@ func (r *ReferenceAdapter) EnsureProjectConfigured() error {
return err
}

r.logger.Info("Configuring APIS")
r.logger.V(int(logtypes.ProjectReference)).Info("Configuring APIS")
err = r.configureAPIS(configMap)
if err != nil {
r.logger.Error(err, "Error configuring APIS")
return err
}

r.logger.Info("Configuring Service Account")
r.logger.V(int(logtypes.ProjectReference)).Info("Configuring Service Account")
err = r.configureServiceAccount()
if err != nil {
r.logger.Error(err, "Error configuring service account")
return err
}

r.logger.Info("Creating Credentials")
r.logger.V(int(logtypes.ProjectReference)).Info("Creating Credentials")
err = r.createCredentials()
if err != nil {
r.logger.Error(err, "Error creating credentials")
Expand All @@ -172,7 +172,7 @@ func (r *ReferenceAdapter) EnsureProjectConfigured() error {

func (r *ReferenceAdapter) EnsureStateReady() error {
if r.ProjectReference.Status.State != gcpv1alpha1.ProjectReferenceStatusReady {
r.logger.Info("Setting Status on projectReference")
r.logger.V(int(logtypes.ProjectReference)).Info("Setting Status on projectReference")
r.ProjectReference.Status.State = gcpv1alpha1.ProjectReferenceStatusReady
return r.kubeClient.Status().Update(context.TODO(), r.ProjectReference)
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func (r *ReferenceAdapter) createProject(parentFolderID string) error {
if projectExists {
switch project.LifecycleState {
case "ACTIVE":
r.logger.Info("Project lifecycleState == ACTIVE")
r.logger.V(int(logtypes.ProjectReference)).Info("Project lifecycleState == ACTIVE")
return nil
case "DELETE_REQUESTED":
return operrors.ErrInactiveProject
Expand All @@ -309,12 +309,12 @@ func (r *ReferenceAdapter) createProject(parentFolderID string) error {
}
}

r.logger.Info("Creating Project")
r.logger.V(int(logtypes.ProjectReference)).Info("Creating Project")
// If we cannot create the project clear the projectID from spec so we can try again with another unique key
_, err = r.gcpClient.CreateProject(parentFolderID)
if err != nil {
r.logger.Error(err, "could not create project", "Parent Folder ID", parentFolderID, "Requested Project ID", r.ProjectReference.Spec.GCPProjectID)
r.logger.Info("Clearing gcpProjectID from ProjectReferenceSpec")
r.logger.V(int(logtypes.ProjectReference)).Info("Clearing gcpProjectID from ProjectReferenceSpec")
//Todo() We need to requeue here ot it will continue to the next step.
err = r.clearProjectID()
if err != nil {
Expand All @@ -340,14 +340,14 @@ func (r *ReferenceAdapter) getProject(projectId string) (*cloudresourcemanager.P
}

func (r *ReferenceAdapter) configureAPIS(config configmap.OperatorConfigMap) error {
r.logger.Info("Enabling Billing API")
r.logger.V(int(logtypes.ProjectReference)).Info("Enabling Billing API")
err := r.gcpClient.EnableAPI(r.ProjectReference.Spec.GCPProjectID, "cloudbilling.googleapis.com")
if err != nil {
r.logger.Error(err, fmt.Sprintf("Error enabling %s api for project %s", "cloudbilling.googleapis.com", r.ProjectReference.Spec.GCPProjectID))
return err
}

r.logger.Info("Linking Cloud Billing Account")
r.logger.V(int(logtypes.ProjectReference)).Info("Linking Cloud Billing Account")
err = r.gcpClient.CreateCloudBillingAccount(r.ProjectReference.Spec.GCPProjectID, config.BillingAccount)
if err != nil {
r.logger.Error(err, "error creating CloudBilling")
Expand Down Expand Up @@ -385,7 +385,7 @@ func (r *ReferenceAdapter) configureServiceAccount() error {
serviceAccount, err := r.gcpClient.GetServiceAccount(osdServiceAccountName)
if err != nil {
// Create OSDManged Service account
r.logger.Info("Creating Service Account")
r.logger.V(int(logtypes.ProjectReference)).Info("Creating Service Account")
account, err := r.gcpClient.CreateServiceAccount(osdServiceAccountName, osdServiceAccountName)
if err != nil {
r.logger.Error(err, "could not create service account", "Service Account Name", osdServiceAccountName)
Expand All @@ -394,7 +394,7 @@ func (r *ReferenceAdapter) configureServiceAccount() error {
serviceAccount = account
}

r.logger.Info("Setting Service Account Policies")
r.logger.V(int(logtypes.ProjectReference)).Info("Setting Service Account Policies")
err = r.SetIAMPolicy(serviceAccount.Email)
if err != nil {
r.logger.Error(err, "could not update policy on project", "Project Name", r.ProjectReference.Spec.GCPProjectID)
Expand All @@ -411,7 +411,7 @@ func (r *ReferenceAdapter) createCredentials() error {
return err
}

r.logger.Info("Creating Service AccountKey")
r.logger.V(int(logtypes.ProjectReference)).Info("Creating Service AccountKey")
key, err := r.gcpClient.CreateServiceAccountKey(serviceAccount.Email)
if err != nil {
r.logger.Error(err, "could not create service account key", "Service Account Name", serviceAccount.Email)
Expand All @@ -430,7 +430,7 @@ func (r *ReferenceAdapter) createCredentials() error {
Name: r.ProjectClaim.Spec.GCPCredentialSecret.Name,
})

r.logger.Info(fmt.Sprintf("Creating Secret %s in namespace %s", r.ProjectClaim.Spec.GCPCredentialSecret.Name, r.ProjectClaim.Spec.GCPCredentialSecret.Namespace))
r.logger.V(int(logtypes.ProjectReference)).Info(fmt.Sprintf("Creating Secret %s in namespace %s", r.ProjectClaim.Spec.GCPCredentialSecret.Name, r.ProjectClaim.Spec.GCPCredentialSecret.Namespace))
createErr := r.kubeClient.Create(context.TODO(), secret)
if createErr != nil {
r.logger.Error(createErr, "could not create service account secret ", "Service Account Secret Name", r.ProjectClaim.Spec.GCPCredentialSecret.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
condition "github.com/openshift/gcp-project-operator/pkg/condition"
"github.com/openshift/gcp-project-operator/pkg/gcpclient"
"github.com/openshift/gcp-project-operator/pkg/util"
logtypes "github.com/openshift/gcp-project-operator/pkg/util/types"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -81,7 +82,6 @@ type ReconcileProjectReference struct {
// Reconcile wraps ReconcileHandler() and updates the conditions if any error occurs
func (r *ReconcileProjectReference) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling ProjectReference")

projectReference := &gcpv1alpha1.ProjectReference{}
err := r.client.Get(context.TODO(), request.NamespacedName, projectReference)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (r *ReconcileProjectReference) ReconcileHandler(adapter *ReferenceAdapter,
}

if adapter.ProjectReference.Spec.GCPProjectID == "" {
reqLogger.Info("Creating ProjectID in ProjectReference CR")
reqLogger.V(int(logtypes.ProjectReference)).Info("Creating ProjectID in ProjectReference CR")
err := adapter.UpdateProjectID()
if err != nil {
reqLogger.Error(err, "Could not update ProjectID in Project Reference CR")
Expand All @@ -165,14 +165,14 @@ func (r *ReconcileProjectReference) ReconcileHandler(adapter *ReferenceAdapter,
return r.requeue()
}

reqLogger.Info("Adding a Finalizer")
reqLogger.V(int(logtypes.ProjectReference)).Info("Adding a Finalizer")
err = adapter.EnsureFinalizerAdded()
if err != nil {
reqLogger.Error(err, "Error adding the finalizer")
return r.requeueOnErr(err)
}

reqLogger.Info("Configuring Project")
reqLogger.V(int(logtypes.ProjectReference)).Info("Configuring Project")
err = adapter.EnsureProjectConfigured()
if err != nil {
return r.requeueAfter(5*time.Second, err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/gcpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

logtypes "github.com/openshift/gcp-project-operator/pkg/util/types"
"golang.org/x/oauth2/google"
cloudbilling "google.golang.org/api/cloudbilling/v1"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
Expand Down Expand Up @@ -157,7 +158,7 @@ func (c *gcpClient) GetProject(projectID string) (*cloudresourcemanager.Project,

// CreateProject creates a project in a given folder.
func (c *gcpClient) CreateProject(parentFolderID string) (*cloudresourcemanager.Operation, error) {
log.Info("gcpClient.CreateProject")
log.V(logtypes.GCPClient).Info("gcpClient.CreateProject")
project := cloudresourcemanager.Project{
//Labels: nil,
Name: c.projectName,
Expand Down Expand Up @@ -281,7 +282,7 @@ func (c *gcpClient) SetIamPolicy(setIamPolicyRequest *cloudresourcemanager.SetIa
}

func (c *gcpClient) EnableAPI(projectID, api string) error {
log.Info(fmt.Sprintf("enable %s api", api))
log.V(logtypes.GCPClient).Info(fmt.Sprintf("enable %s api", api))
enableServicerequest := &serviceManagment.EnableServiceRequest{
ConsumerId: fmt.Sprintf("project:%s", projectID),
}
Expand All @@ -302,7 +303,7 @@ func (c *gcpClient) EnableAPI(projectID, api string) error {
// creation is completed and marked as Done.
// Something is not propagating in the backend.
if ok && ae.Code == http.StatusForbidden && retry <= gcpAPIRetriesCount {
log.Info(fmt.Sprintf("retry %d for enable %s api", retry, api))
log.V(logtypes.GCPClient).Info(fmt.Sprintf("retry %d for enable %s api", retry, api))
continue
}
return err
Expand Down
Loading

0 comments on commit dcb3ff8

Please sign in to comment.