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

Update to go1.23.1 #65

Draft
wants to merge 64 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
4f31f81
Add cache objects to facade.client and cf.client
santiago-ventura Aug 12, 2024
fdbce79
Add initial logic for caching resources
santiago-ventura Aug 12, 2024
5d28fbe
fix some errors in code
santiago-ventura Aug 12, 2024
860820d
Add logic in cf client for populating the cf cache
santiago-ventura Aug 12, 2024
30a47ef
GetInstance function retrieving instance from Canch
santiago-ventura Aug 13, 2024
402e470
"Add go routine for cache execution. Include use of env variable for …
santiago-ventura Aug 13, 2024
21011cb
Check for nil and ensure resourcesCache is initialized
santiago-ventura Aug 19, 2024
9df5828
changes to client.go to refresh go routine
shilparamasamyreddy Aug 19, 2024
e625b82
added channel for cache refresh
shilparamasamyreddy Aug 19, 2024
9a5bef6
remove no needed comment
santiago-ventura Aug 20, 2024
824a3bf
correct typo
santiago-ventura Aug 20, 2024
1d3c1a6
populate cache on demand based on expiry
shilparamasamyreddy Aug 21, 2024
8df71cb
Refactoring Cache objects and adding more public functions.
santiago-ventura Aug 21, 2024
f866ead
Added config package to read env variables
shilparamasamyreddy Aug 21, 2024
4ee8437
Adjusted the existing tests
shilparamasamyreddy Aug 22, 2024
9d1041c
added licence header in config
shilparamasamyreddy Aug 22, 2024
acfd78c
added logic to remove the deleted instance from cache
shilparamasamyreddy Aug 23, 2024
edc8ddb
init instance to return owner for adopt instance scenario
shilparamasamyreddy Aug 26, 2024
16e2d61
Code for update instance to cache
shilparamasamyreddy Aug 28, 2024
061f668
Added logging for testing
shilparamasamyreddy Aug 29, 2024
85e0578
Merge branch 'main' into cf-cache-resources
RalfHammer Aug 29, 2024
80b6a84
tests for resource cache
shilparamasamyreddy Sep 2, 2024
e750753
refactored the code
shilparamasamyreddy Sep 3, 2024
b50e1a6
cf/client.go changes for resource cache
shilparamasamyreddy Sep 3, 2024
e327ccd
Client tests are added for populate resource cache
shilparamasamyreddy Sep 3, 2024
4e28306
small refactoring the code
shilparamasamyreddy Sep 3, 2024
c5e2fd4
refactoring to populate resource cache function and few small changes
shilparamasamyreddy Sep 4, 2024
070ad27
update populateResourceCache
RalfHammer Sep 5, 2024
8fdff6d
improve testing of expectations
RalfHammer Sep 5, 2024
d8f0bdb
minor improvements
RalfHammer Sep 5, 2024
b03b198
improve documentation in resourcecache
RalfHammer Sep 5, 2024
554832e
minor improvements to config
RalfHammer Sep 5, 2024
f86cf76
Modified resource cache tests
shilparamasamyreddy Sep 6, 2024
6d642c8
Added caching logic for space and binding
shilparamasamyreddy Sep 10, 2024
3534115
interface to manage populate cache for all resources
shilparamasamyreddy Sep 10, 2024
03806e9
add logging for testing
shilparamasamyreddy Sep 11, 2024
cc67ad6
client and unit tests for cache
shilparamasamyreddy Sep 12, 2024
018d734
caching implementation and tests for space user role relationship
shilparamasamyreddy Sep 16, 2024
b9b7062
added todo for improvements
shilparamasamyreddy Sep 16, 2024
2cb5008
Make DeleteSpace consistent with other methods
RalfHammer Sep 18, 2024
be24a05
add config also to SpaceReconciler
RalfHammer Sep 18, 2024
12252c7
use consistent namings
RalfHammer Sep 18, 2024
277feef
remove debug log, harmonize coding
RalfHammer Sep 18, 2024
717817f
update official documentation with the new cache feature
santiago-ventura Sep 18, 2024
6a2214f
Update operator.md
santiago-ventura Sep 19, 2024
4467f58
Update based on review comments
santiago-ventura Sep 19, 2024
6c99dc4
remove error in markdown
santiago-ventura Sep 19, 2024
11a962b
simplify/document populate functions
RalfHammer Sep 19, 2024
863e518
remove not needed length check
RalfHammer Sep 19, 2024
443f26c
Add TODO for keys in shared resource cache
RalfHammer Sep 19, 2024
109a262
health.go uses the cache to retrieve the space
santiago-ventura Sep 20, 2024
5fbc0ad
small fix to mutex
shilparamasamyreddy Sep 20, 2024
a092be5
modified the global cfResourceCache logic
shilparamasamyreddy Sep 26, 2024
cb8068a
remove temporary log messages
RalfHammer Sep 26, 2024
781fb58
enable resource cache in launch.json
RalfHammer Sep 26, 2024
f1d443c
remove unneccessary type
RalfHammer Sep 27, 2024
233448e
fix typo
RalfHammer Sep 27, 2024
a770254
fix linting issue
RalfHammer Oct 2, 2024
6ce7e98
get details for credentials on demand
RalfHammer Oct 2, 2024
445aea1
add logging
RalfHammer Oct 2, 2024
1eea3f2
fix error message
RalfHammer Oct 7, 2024
bd782cf
Improve documentation about caching
RalfHammer Oct 7, 2024
f8da71c
update go version
RalfHammer Oct 7, 2024
0e37bb7
remove unused functions
RalfHammer Oct 7, 2024
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
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"env": {
"RESOURCE_CACHE_ENABLED": "true"
},
"args": [
"--kubeconfig=${workspaceFolder}/.kubeconfig",
"--webhook-bind-address=:2443",
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.23.0 as builder
FROM --platform=$BUILDPLATFORM golang:1.23.1 AS builder
ARG TARGETOS
ARG TARGETARCH

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.14.0
CODE_GENERATOR_VERSION ?= v0.23.4
COUNTERFEITER_VERSION ?= v6.8.1
GOLINT_VERSION ?= v1.57.1
GOLINT_VERSION ?= v1.61.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/sap/cf-service-operator

go 1.22.5
go 1.23.1

require (
github.com/cloudfoundry-community/go-cfclient/v3 v3.0.0-alpha.5
Expand All @@ -19,6 +19,7 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/caarlos0/env/v11 v11.2.2
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down
145 changes: 94 additions & 51 deletions internal/cf/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ func (bo *bindingFilterOwner) getListOptions() *cfclient.ServiceCredentialBindin
// If multiple bindings are found, an error is returned.
// The function add the parameter values to the orphan cf binding, so that can be adopted.
func (c *spaceClient) GetBinding(ctx context.Context, bindingOpts map[string]string) (*facade.Binding, error) {
if c.resourceCache.checkResourceCacheEnabled() {
// attempt to retrieve binding from cache
if c.resourceCache.isCacheExpired(bindingType) {
populateResourceCache(c, bindingType, "")
}
binding, inCache := c.resourceCache.getBindingFromCache(bindingOpts["owner"])
if inCache {
return binding, nil
}
}

// attempt to retrieve binding from Cloud Foundry
var filterOpts bindingFilter
if bindingOpts["name"] != "" {
filterOpts = &bindingFilterName{name: bindingOpts["name"]}
Expand All @@ -65,62 +77,17 @@ func (c *spaceClient) GetBinding(ctx context.Context, bindingOpts map[string]str
} else if len(serviceBindings) > 1 {
return nil, errors.New(fmt.Sprintf("found multiple service bindings with owner: %s", bindingOpts["owner"]))
}

serviceBinding := serviceBindings[0]

// add parameter values to the cf orphan binding
// add parameter values to the orphaned binding in Cloud Foundry
if bindingOpts["name"] != "" {
generationvalue := "0"
serviceBinding.Metadata.Annotations[annotationGeneration] = &generationvalue
parameterHashValue := "0"
serviceBinding.Metadata.Annotations[annotationGeneration] = &generationvalue
serviceBinding.Metadata.Annotations[annotationParameterHash] = &parameterHashValue
}

guid := serviceBinding.GUID
name := serviceBinding.Name
generation, err := strconv.ParseInt(*serviceBinding.Metadata.Annotations[annotationGeneration], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "error parsing service binding generation")
}
parameterHash := *serviceBinding.Metadata.Annotations[annotationParameterHash]
var state facade.BindingState
switch serviceBinding.LastOperation.Type + ":" + serviceBinding.LastOperation.State {
case "create:in progress":
state = facade.BindingStateCreating
case "create:succeeded":
state = facade.BindingStateReady
case "create:failed":
state = facade.BindingStateCreatedFailed
case "delete:in progress":
state = facade.BindingStateDeleting
case "delete:succeeded":
state = facade.BindingStateDeleted
case "delete:failed":
state = facade.BindingStateDeleteFailed
default:
state = facade.BindingStateUnknown
}
stateDescription := serviceBinding.LastOperation.Description

var credentials map[string]interface{}
if state == facade.BindingStateReady {
details, err := c.client.ServiceCredentialBindings.GetDetails(ctx, guid)
if err != nil {
return nil, errors.Wrap(err, "error getting service binding details")
}
credentials = details.Credentials
}

return &facade.Binding{
Guid: guid,
Name: name,
Owner: bindingOpts["owner"],
Generation: generation,
ParameterHash: parameterHash,
State: state,
StateDescription: stateDescription,
Credentials: credentials,
}, nil
return c.InitBinding(ctx, serviceBinding, bindingOpts)
}

// Required parameters (may not be initial): name, serviceInstanceGuid, owner, generation
Expand All @@ -144,7 +111,7 @@ func (c *spaceClient) CreateBinding(ctx context.Context, name string, serviceIns
}

// Required parameters (may not be initial): guid, generation
func (c *spaceClient) UpdateBinding(ctx context.Context, guid string, generation int64, parameters map[string]interface{}) error {
func (c *spaceClient) UpdateBinding(ctx context.Context, guid string, owner string, generation int64, parameters map[string]interface{}) error {
// TODO: why is there no cfresource.NewServiceCredentialBindingUpdate() method ?
req := &cfresource.ServiceCredentialBindingUpdate{}
req.Metadata = cfresource.NewMetadata().
Expand All @@ -157,9 +124,85 @@ func (c *spaceClient) UpdateBinding(ctx context.Context, guid string, generation
}
}
_, err := c.client.ServiceCredentialBindings.Update(ctx, guid, req)

// update binding in cache
if err == nil && c.resourceCache.checkResourceCacheEnabled() {
isUpdated := c.resourceCache.updateBindingInCache(owner, parameters, generation)
if !isUpdated {
// add binding to cache if it is not found
// TODO: why getting binding here but not instance in CreateInstance() ?
binding, err := c.GetBinding(ctx, map[string]string{"owner": owner})
if err != nil {
return err
}
c.resourceCache.addBindingInCache(owner, binding)
}

}
return err
}

func (c *spaceClient) DeleteBinding(ctx context.Context, guid string) error {
return c.client.ServiceCredentialBindings.Delete(ctx, guid)
func (c *spaceClient) DeleteBinding(ctx context.Context, guid string, owner string) error {
err := c.client.ServiceCredentialBindings.Delete(ctx, guid)

// delete binding from cache
if err == nil && c.resourceCache.checkResourceCacheEnabled() {
c.resourceCache.deleteBindingFromCache(owner)
}

return err
}

// InitBinding wraps cfclient.ServiceCredentialBinding as a facade.Binding.
func (c *spaceClient) InitBinding(ctx context.Context, serviceBinding *cfresource.ServiceCredentialBinding, bindingOpts map[string]string) (*facade.Binding, error) {
generation, err := strconv.ParseInt(*serviceBinding.Metadata.Annotations[annotationGeneration], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "error parsing service binding generation")
}

owner := bindingOpts["owner"]
if owner == "" {
owner = *serviceBinding.Metadata.Labels[labelOwner]
}

var state facade.BindingState
switch serviceBinding.LastOperation.Type + ":" + serviceBinding.LastOperation.State {
case "create:in progress":
state = facade.BindingStateCreating
case "create:succeeded":
state = facade.BindingStateReady
case "create:failed":
state = facade.BindingStateCreatedFailed
case "delete:in progress":
state = facade.BindingStateDeleting
case "delete:succeeded":
state = facade.BindingStateDeleted
case "delete:failed":
state = facade.BindingStateDeleteFailed
default:
state = facade.BindingStateUnknown
}

guid := serviceBinding.GUID

return &facade.Binding{
Guid: guid,
Name: serviceBinding.Name,
Owner: owner,
Generation: generation,
ParameterHash: *serviceBinding.Metadata.Annotations[annotationParameterHash],
State: state,
StateDescription: serviceBinding.LastOperation.Description,
Credentials: nil, // filled later
}, nil
}

func (c *spaceClient) FillBindingDetails(ctx context.Context, binding *facade.Binding) error {
details, err := c.client.ServiceCredentialBindings.GetDetails(ctx, binding.Guid)
if err != nil {
return errors.Wrap(err, "error getting service binding details")
}
binding.Credentials = details.Credentials

return nil
}
Loading