Skip to content

Commit

Permalink
api: Add Forklift API to scheme during initialization
Browse files Browse the repository at this point in the history
... rather than when the hooks are called to avoid "concurrent map read
and map write" error.

Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Aug 31, 2023
1 parent ed39f4e commit a6845b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
2 changes: 2 additions & 0 deletions cmd/forklift-api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ go_library(
importpath = "github.com/konveyor/forklift-controller/cmd/forklift-api",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis",
"//pkg/forklift-api",
"//pkg/lib/logging",
"//vendor/github.com/go-logr/logr",
"//vendor/k8s.io/client-go/kubernetes/scheme",
"//vendor/sigs.k8s.io/controller-runtime/pkg/log",
],
)
Expand Down
11 changes: 11 additions & 0 deletions cmd/forklift-api/forklift-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ limitations under the License.
package main

import (
"os"

"github.com/go-logr/logr"
"github.com/konveyor/forklift-controller/pkg/apis"
forklift_api "github.com/konveyor/forklift-controller/pkg/forklift-api"
"github.com/konveyor/forklift-controller/pkg/lib/logging"
"k8s.io/client-go/kubernetes/scheme"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -34,5 +38,12 @@ func init() {
func main() {
log.Info("start forklift-api")
app := forklift_api.NewForkliftApi()

err := apis.AddToScheme(scheme.Scheme)
if err != nil {
log.Error(err, "unable to add forklift API to scheme")
os.Exit(1)
}

app.Execute()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net/http"

"github.com/konveyor/forklift-controller/pkg/apis"
api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
adapter "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/vsphere"
"github.com/konveyor/forklift-controller/pkg/controller/provider/container"
Expand Down Expand Up @@ -163,17 +162,6 @@ func (admitter *SecretAdmitter) testConnectionToHost(hostName string) (tested bo
return
}

err = api.SchemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.Error(err, "Couldn't build the scheme")
return
}
err = apis.AddToScheme(scheme.Scheme)
if err != nil {
log.Error(err, "Couldn't add forklift API to the scheme")
return
}

cl, err := client.New(config, client.Options{Scheme: scheme.Scheme})
if err != nil {
log.Error(err, "Couldn't create a cluster client")
Expand Down Expand Up @@ -243,24 +231,12 @@ func (admitter *SecretAdmitter) validateUpdateOfOVAProviderSecret() *admissionv1
}

func (admitter *SecretAdmitter) isOvaUrlChanged() (bool, error) {

config, err := rest.InClusterConfig()
if err != nil {
log.Error(err, "Couldn't get the cluster configuration")
return false, err
}

err = api.SchemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.Error(err, "Couldn't build the scheme")
return false, err
}
err = apis.AddToScheme(scheme.Scheme)
if err != nil {
log.Error(err, "Couldn't add forklift API to the scheme")
return false, err
}

cl, err := client.New(config, client.Options{Scheme: scheme.Scheme})
if err != nil {
log.Error(err, "Couldn't create a cluster client")
Expand Down

0 comments on commit a6845b8

Please sign in to comment.