Skip to content

Commit

Permalink
Merge pull request openstack-k8s-operators#10 from slagle/api-endpoint
Browse files Browse the repository at this point in the history
Use default from route for initial endpoint
  • Loading branch information
slagle authored Jul 21, 2020
2 parents 8a534ca + 6486178 commit 7c41451
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 0 additions & 2 deletions deploy/crds/keystone.openstack.org_keystoneapis_cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ spec:
replicas: 1
databasePassword: foobar123
databaseHostname: openstack-db-mariadb
# used for keystone-manage bootstrap endpoints
apiEndpoint: http://keystone-test.apps.test.dprince/
# used to create the DB schema
databaseAdminUsername: root
databaseAdminPassword: foobar123
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/keystone/v1/keystoneapi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type KeystoneAPISpec struct {
DatabaseAdminPassword string `json:"databaseAdminPassword,omitempty"`
// Keystone API Admin Password
AdminPassword string `json:"adminPassword,omitempty"`
// Keystone API Endpoint, the http/https route configured to access the Keystone API
APIEndpoint string `json:"apiEndpoint,omitempty"`
// Replicas
Replicas int32 `json:"replicas"`
}
Expand Down
60 changes: 36 additions & 24 deletions pkg/controller/keystoneapi/keystoneapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
"strings"
"time"
)

Expand Down Expand Up @@ -248,8 +249,41 @@ func (r *ReconcileKeystoneAPI) Reconcile(request reconcile.Request) (reconcile.R
return reconcile.Result{}, err
}

// Create the route if none exists
route := keystone.Route(instance, instance.Name)

// Set Keystone instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, route, r.scheme); err != nil {
return reconcile.Result{}, err
}

// Check if this Route already exists
foundRoute := &routev1.Route{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: route.Name, Namespace: route.Namespace}, foundRoute)
if err != nil && k8s_errors.IsNotFound(err) {
reqLogger.Info("Creating a new Route", "Route.Namespace", route.Namespace, "Route.Name", route.Name)
err = r.client.Create(context.TODO(), route)
if err != nil {
return reconcile.Result{}, err
}

return reconcile.Result{RequeueAfter: time.Second * 5}, err
} else if err != nil {
return reconcile.Result{}, err
}

// Define a new BootStrap Job object
bootstrapJob := keystone.BootstrapJob(instance, instance.Name)

// Look at the generated route to get the value for the initial endpoint
// TODO (slagle): support/assume https
var apiEndpoint string
if !strings.HasPrefix(foundRoute.Spec.Host, "http") {
apiEndpoint = fmt.Sprintf("http://%s", foundRoute.Spec.Host)
} else {
apiEndpoint = foundRoute.Spec.Host
}

bootstrapJob := keystone.BootstrapJob(instance, instance.Name, apiEndpoint)
bootstrapHash, err := util.ObjectHash(bootstrapJob)
if err != nil {
return reconcile.Result{}, fmt.Errorf("error calculating bootstrap hash: %v", err)
Expand All @@ -268,36 +302,14 @@ func (r *ReconcileKeystoneAPI) Reconcile(request reconcile.Request) (reconcile.R
return reconcile.Result{RequeueAfter: time.Second * 5}, err
}
}
// db sync completed... okay to store the hash to disable it
// bootstrap completed... okay to store the hash to disable it
r.setBootstrapHash(instance, bootstrapHash)
// delete the job
requeue, err = DeleteJob(bootstrapJob, r, reqLogger)
if err != nil {
return reconcile.Result{}, err
}

// Create the route if none exists
route := keystone.Route(instance, instance.Name)

// Set Keystone instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, route, r.scheme); err != nil {
return reconcile.Result{}, err
}

// Check if this Route already exists
foundRoute := &routev1.Route{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: route.Name, Namespace: route.Namespace}, foundRoute)
if err != nil && k8s_errors.IsNotFound(err) {
reqLogger.Info("Creating a new Route", "Route.Namespace", route.Namespace, "Route.Name", route.Name)
err = r.client.Create(context.TODO(), route)
if err != nil {
return reconcile.Result{}, err
}

return reconcile.Result{RequeueAfter: time.Second * 5}, err
} else if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil

}
Expand Down
4 changes: 2 additions & 2 deletions pkg/keystone/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type bootstrapOptions struct {
}

// BootstrapJob func
func BootstrapJob(cr *comv1.KeystoneAPI, configMapName string) *batchv1.Job {
func BootstrapJob(cr *comv1.KeystoneAPI, configMapName string, APIEndpoint string) *batchv1.Job {

// NOTE: as a convention the configmap is name the same as the service
opts := bootstrapOptions{cr.Spec.AdminPassword, cr.Spec.APIEndpoint, configMapName}
opts := bootstrapOptions{cr.Spec.AdminPassword, APIEndpoint, configMapName}
runAsUser := int64(0)

job := &batchv1.Job{
Expand Down

0 comments on commit 7c41451

Please sign in to comment.