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

[Misc] Enabled JSON Logs & Added/Enhanced structured logs #77

Merged
merged 6 commits into from
May 24, 2024
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
1 change: 1 addition & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
)

func main() {
klog.SetLogger(util.GetLogger())
config := util.GetConfig()
if config == nil {
klog.Fatal("Config not found")
Expand Down
1 change: 1 addition & 0 deletions cmd/mtx-job/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type OAuthResponse struct {
}

func main() {
klog.SetLogger(util.GetLogger())
os.Exit(execute())
}

Expand Down
37 changes: 19 additions & 18 deletions cmd/server/internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s *SubscriptionHandler) CreateTenant(req *http.Request) *Result {
return &Result{Tenant: nil, Message: err.Error()}
}

klog.InfoS("Creating Tenant")
klog.InfoS("Creating tenant", "caName", ca.Name, "namespace", ca.Namespace, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
tenant, _ = s.Clientset.SmeV1alpha1().CAPTenants(ca.Namespace).Create(context.TODO(), &v1alpha1.CAPTenant{
ObjectMeta: metav1.ObjectMeta{
GenerateName: ca.Name + "-",
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s *SubscriptionHandler) CreateTenant(req *http.Request) *Result {
return ResourceFound
}
}
klog.V(2).InfoS("Done with create", "message", message(created), "tenant", tenant)
klog.V(2).InfoS("Tenant successfully created", "message", message(created), "tenant", tenant, "caName", ca.Name, "namespace", ca.Namespace, "tenant", tenant, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
return &Result{Tenant: tenant, Message: message(created)}
}

Expand All @@ -210,21 +210,21 @@ func (s *SubscriptionHandler) getTenant(globalAccountGUID string, btpAppName str
LabelTenantId: tenantId,
})
if err != nil {
klog.ErrorS(err, "Error occurred in getTenant")
klog.ErrorS(err, "Error occurred in getTenant", "namespace", namespace, "tenantId", tenantId, LabelBTPApplicationIdentifierHash, sha1Sum(globalAccountGUID, btpAppName))
return &Result{Tenant: nil, Message: err.Error()}
}

ctList, err := s.Clientset.SmeV1alpha1().CAPTenants(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector.String()})
if err != nil {
klog.ErrorS(err, "Error occurred in getTenant")
klog.ErrorS(err, "Error occurred in getTenant", "namespace", namespace, "tenantId", tenantId, LabelBTPApplicationIdentifierHash, sha1Sum(globalAccountGUID, btpAppName))
return &Result{Tenant: nil, Message: err.Error()}
}
if len(ctList.Items) == 0 {
klog.InfoS("No tenant found")
klog.InfoS("No tenant found", "namespace", namespace, "tenantId", tenantId, LabelBTPApplicationIdentifierHash, sha1Sum(globalAccountGUID, btpAppName))
return &Result{Tenant: nil, Message: ResourceNotFound}
}
// Assume only 1 tenant actually matches the selector!
klog.V(2).InfoS("Tenant found", v1alpha1.CAPApplicationKind, &ctList.Items[0])
klog.V(2).InfoS("Tenant found", v1alpha1.CAPApplicationKind, &ctList.Items[0], "namespace", namespace, "tenantId", tenantId, LabelBTPApplicationIdentifierHash, sha1Sum(globalAccountGUID, btpAppName))
return &Result{Tenant: &ctList.Items[0], Message: ResourceFound}
}

Expand Down Expand Up @@ -255,6 +255,7 @@ func (s *SubscriptionHandler) DeleteTenant(req *http.Request) *Result {
// validate token
err = s.checkAuthorization(req.Header.Get("Authorization"), saasData, uaaData)
if err != nil {
klog.ErrorS(err, "Error occurred in checkAuthorization", LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
return &Result{Tenant: nil, Message: err.Error()}
}

Expand All @@ -263,10 +264,10 @@ func (s *SubscriptionHandler) DeleteTenant(req *http.Request) *Result {
tenantName := ResourceNotFound
if tenant != nil {
tenantName = tenant.Name
klog.InfoS("Tenant found, deleting")
klog.InfoS("Tenant found, deleting", "tenant", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
err = s.Clientset.SmeV1alpha1().CAPTenants(tenant.Namespace).Delete(context.TODO(), tenant.Name, metav1.DeleteOptions{})
if err != nil {
klog.ErrorS(err, "Error deleting tenant")
klog.ErrorS(err, "Error deleting tenant", "tenant", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
return &Result{Tenant: nil, Message: err.Error()}
}
}
Expand Down Expand Up @@ -308,7 +309,7 @@ func (s *SubscriptionHandler) checkAuthorization(authHeader string, saasData *ut
RequiredScopes: []string{uaaData.XSAppName + ".Callback", uaaData.XSAppName + ".mtcallback"},
}, s.httpClientGenerator.NewHTTPClient())
if err != nil {
klog.ErrorS(err, "failed token validation")
klog.ErrorS(err, "failed token validation", "XSAppName", uaaData.XSAppName)
return errors.New(AuthorizationCheckFailed)
}
return nil
Expand All @@ -317,17 +318,17 @@ func (s *SubscriptionHandler) checkAuthorization(authHeader string, saasData *ut
func (s *SubscriptionHandler) initializeCallback(tenantName string, ca *v1alpha1.CAPApplication, saasData *util.SaasRegistryCredentials, req *http.Request, tenantSubDomain string, isProvisioning bool) {
appUrl := "https://" + tenantSubDomain + "." + ca.Spec.Domains.Primary
asyncCallbackPath := req.Header.Get("STATUS_CALLBACK")
klog.InfoS("callback initialized", "subscription URL", appUrl, "async callback path", asyncCallbackPath)
klog.InfoS("callback initialized", "subscription URL", appUrl, "async callback path", asyncCallbackPath, "tenantId", ca.Spec.Provider.TenantId, "tenantName", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])

go func() {
// create a context for tenant checks and outgoing requests
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Check tenant status asynchronously
klog.InfoS("Waiting for tenant status check...")
klog.InfoS("Waiting for tenant status check...", "tenantId", ca.Spec.Provider.TenantId, "tenantName", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
status := s.checkCAPTenantStatus(ctx, ca.Namespace, tenantName, isProvisioning, saasData.CallbackTimeoutMillis)
klog.InfoS("CAPTenant check complete", "status", status)
klog.InfoS("CAPTenant check complete", "status", status, "tenantId", ca.Spec.Provider.TenantId, "tenantName", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])

additionalOutput := &map[string]any{}
if isProvisioning {
Expand All @@ -346,7 +347,7 @@ func (s *SubscriptionHandler) initializeCallback(tenantName string, ca *v1alpha1
s.handleAsyncCallback(ctx, saasData, status, asyncCallbackPath, appUrl, additionalOutput, isProvisioning)
}()

klog.InfoS("Waiting for async saas callback after checks...")
klog.InfoS("Waiting for async saas callback after checks...", "tenantId", ca.Spec.Provider.TenantId, "tenantName", tenantName, LabelBTPApplicationIdentifierHash, ca.Labels[LabelBTPApplicationIdentifierHash])
}

func (s *SubscriptionHandler) checkCAPTenantStatus(ctx context.Context, tenantNamespace string, tenantName string, provisioning bool, callbackTimeoutMs string) bool {
Expand All @@ -366,15 +367,15 @@ func (s *SubscriptionHandler) checkCAPTenantStatus(ctx context.Context, tenantNa
default:
capTenant, err := s.Clientset.SmeV1alpha1().CAPTenants(tenantNamespace).Get(context.TODO(), tenantName, metav1.GetOptions{})
if k8sErrors.IsNotFound(err) {
klog.InfoS("No tenant found.. Exiting CAPTenant status check.")
klog.InfoS("No tenant found.. Exiting CAPTenant status check.", "tenantName", tenantName, "namespace", tenantNamespace)
if !provisioning {
return true
}
}
if capTenant != nil {
klog.InfoS("CAPTenant Found", "Tenantid", capTenant.Spec.TenantId, "status", capTenant.Status.State)
klog.InfoS("CAPTenant Found", "tenantid", capTenant.Spec.TenantId, "status", capTenant.Status.State, LabelBTPApplicationIdentifierHash, capTenant.Labels[LabelBTPApplicationIdentifierHash])
if provisioning && (capTenant.Status.State == v1alpha1.CAPTenantStateReady || capTenant.Status.State == v1alpha1.CAPTenantStateProvisioningError) {
klog.InfoS("Exiting CAPTenant status check", "result status", capTenant.Status.State)
klog.InfoS("Exiting CAPTenant status check", "tenantid", capTenant.Spec.TenantId, "result status", capTenant.Status.State, LabelBTPApplicationIdentifierHash, capTenant.Labels[LabelBTPApplicationIdentifierHash])
return capTenant.Status.State == v1alpha1.CAPTenantStateReady
}
}
Expand Down Expand Up @@ -414,7 +415,7 @@ func (s *SubscriptionHandler) getSaasDetails(capApp *v1alpha1.CAPApplication) *u
result, err = util.ReadServiceCredentialsFromSecret[util.SaasRegistryCredentials](info, capApp.Namespace, s.KubeClienset)
}
if err != nil {
klog.ErrorS(err, "SaaS Registry credentials could not be read. Exiting..")
klog.ErrorS(err, "SaaS Registry credentials could not be read. Exiting..", "tenantId", capApp.Spec.Provider.TenantId, LabelBTPApplicationIdentifierHash, capApp.Labels[LabelBTPApplicationIdentifierHash])
}
return result
}
Expand All @@ -434,7 +435,7 @@ func (s *SubscriptionHandler) getXSUAADetails(capApp *v1alpha1.CAPApplication) *
}

if err != nil {
klog.ErrorS(err, "XSUAA credentials could not be read. Exiting..")
klog.ErrorS(err, "XSUAA credentials could not be read. Exiting..", LabelBTPApplicationIdentifierHash, capApp.Labels[LabelBTPApplicationIdentifierHash])
}
return result
}
Expand Down
1 change: 1 addition & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

func main() {
klog.SetLogger(util.GetLogger())
subHandler := getSubscriptionHandler()
http.HandleFunc("/provision/", subHandler.HandleRequest)

Expand Down
1 change: 1 addition & 0 deletions cmd/web-hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ServerParameters struct {
var parameters ServerParameters

func main() {
klog.SetLogger(util.GetLogger())
// check env for relevant values
portEnv := os.Getenv("WEBHOOK_PORT")
port := 8443
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ require (
github.com/cert-manager/cert-manager v1.14.5
github.com/gardener/cert-management v0.14.2
github.com/gardener/external-dns-management v0.18.5
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/lestrrat-go/jwx/v2 v2.0.21
go.uber.org/zap v1.26.0
golang.org/x/mod v0.17.0
google.golang.org/protobuf v1.34.1
istio.io/api v1.22.0
Expand All @@ -27,7 +30,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
Expand All @@ -52,6 +54,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/net v0.25.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/gardener/external-dns-management v0.18.5 h1:lqKVuuPTPzw5zhYtHtdETSVyx
github.com/gardener/external-dns-management v0.18.5/go.mod h1:6WO+q0vfcxlGjAIeXG9hNr6ve6NHNw1E9Ey8mnGax8Q=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=
Expand Down Expand Up @@ -105,6 +107,12 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
9 changes: 8 additions & 1 deletion internal/controller/reconcile-capapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
}, metav1.CreateOptions{})

// Create provider tenant
klog.InfoS("Processing CAPApplication - Creating Provider tenant", "name", ca.Name, "namespace", ca.Namespace, "tenantId", ca.Spec.Provider.TenantId, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))

if tenant, err = c.crdClient.SmeV1alpha1().CAPTenants(ca.Namespace).Create(
ctx, &v1alpha1.CAPTenant{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -362,7 +364,7 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
}

msg := fmt.Sprintf("provider %v not ready for %v %v.%v; waiting for it to be ready", v1alpha1.CAPTenantKind, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
klog.InfoS(msg, "provider tenant", tenant, v1alpha1.CAPApplicationKind, ca)
klog.InfoS(msg, "provider tenant", tenant.Name, v1alpha1.CAPApplicationKind, ca, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateProcessing, metav1.ConditionFalse, EventActionProviderTenantProcessing, msg)
return true, nil
}
Expand All @@ -372,22 +374,27 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,

func (c *Controller) handleCAPApplicationDeletion(ctx context.Context, ca *v1alpha1.CAPApplication) (*ReconcileResult, error) {
var err error

klog.InfoS("Deleting CAPApplication", "name", ca.Name, "namespace", ca.Namespace, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))
if ca.Status.State != v1alpha1.CAPApplicationStateDeleting {
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateDeleting, metav1.ConditionFalse, "DeleteTriggered", "")
return NewReconcileResultWithResource(ResourceCAPApplication, ca.Name, ca.Namespace, 0), nil
}

// TODO: cleanup domain resources via reconciliation
klog.InfoS("Deleting CAPApplication - Primary Domain Certificate", "name", ca.Name, "namespace", ca.Namespace, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))
if err = c.deletePrimaryDomainCertificate(ctx, ca); err != nil && !k8sErrors.IsNotFound(err) {
return nil, err
}

// delete CAPTenants - return if found in this loop, to verify deletion
var tenantFound bool
klog.InfoS("Deleting CAPApplication - CAPTenants", "name", ca.Name, "namespace", ca.Namespace, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))
if tenantFound, err = c.deleteTenants(ctx, ca); tenantFound || err != nil {
return nil, err
}

klog.InfoS("Deleting CAPApplication - Secrets", "name", ca.Name, "namespace", ca.Namespace, LabelBTPApplicationIdentifierHash, sha256Sum(ca.Spec.GlobalAccountId, ca.Spec.BTPAppName))
if err = c.cleanupPreservedSecrets(ca.Spec.BTP.Services, ca.Namespace); err != nil && !k8sErrors.IsNotFound(err) {
return nil, err
}
Expand Down
Loading
Loading