74 lines
2.7 KiB
Go
74 lines
2.7 KiB
Go
/*
|
|
Copyright 2026.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package controller
|
|
|
|
import databasev1alpha1 "git.ddupan.top/panxiao81/postgresql-tenant-operator/api/v1alpha1"
|
|
|
|
type instancePhaseResult struct {
|
|
phase databasev1alpha1.PostgreSQLInstancePhase
|
|
reconcilingMessage string
|
|
}
|
|
|
|
type instancePhaseHandler func(*databasev1alpha1.PostgreSQLInstance) instancePhaseResult
|
|
|
|
type instanceStateMachine struct {
|
|
handlers map[databasev1alpha1.PostgreSQLInstancePhase]instancePhaseHandler
|
|
}
|
|
|
|
func newInstanceStateMachine() instanceStateMachine {
|
|
return instanceStateMachine{handlers: map[databasev1alpha1.PostgreSQLInstancePhase]instancePhaseHandler{
|
|
databasev1alpha1.PostgreSQLInstancePhasePending: reconcileInstancePending,
|
|
databasev1alpha1.PostgreSQLInstancePhaseValidating: keepInstancePhase,
|
|
databasev1alpha1.PostgreSQLInstancePhaseInitializingRegistry: keepInstancePhase,
|
|
databasev1alpha1.PostgreSQLInstancePhaseReady: keepInstancePhase,
|
|
databasev1alpha1.PostgreSQLInstancePhaseDeleting: reconcileInstanceDeleting,
|
|
}}
|
|
}
|
|
|
|
func (m instanceStateMachine) reconcile(instance *databasev1alpha1.PostgreSQLInstance) instancePhaseResult {
|
|
phase := instance.Status.Phase
|
|
if !instance.DeletionTimestamp.IsZero() {
|
|
phase = databasev1alpha1.PostgreSQLInstancePhaseDeleting
|
|
} else if phase == "" {
|
|
phase = databasev1alpha1.PostgreSQLInstancePhasePending
|
|
}
|
|
|
|
handler, found := m.handlers[phase]
|
|
if !found {
|
|
return instancePhaseResult{phase: phase}
|
|
}
|
|
return handler(instance)
|
|
}
|
|
|
|
func reconcileInstancePending(*databasev1alpha1.PostgreSQLInstance) instancePhaseResult {
|
|
return instancePhaseResult{
|
|
phase: databasev1alpha1.PostgreSQLInstancePhaseValidating,
|
|
reconcilingMessage: "instance dependencies are being validated",
|
|
}
|
|
}
|
|
|
|
func reconcileInstanceDeleting(*databasev1alpha1.PostgreSQLInstance) instancePhaseResult {
|
|
return instancePhaseResult{
|
|
phase: databasev1alpha1.PostgreSQLInstancePhaseDeleting,
|
|
reconcilingMessage: "instance deletion is reconciling",
|
|
}
|
|
}
|
|
|
|
func keepInstancePhase(instance *databasev1alpha1.PostgreSQLInstance) instancePhaseResult {
|
|
return instancePhaseResult{phase: instance.Status.Phase}
|
|
}
|