Oc-Datacenter Allowed Resource And Prepull Images For Efficient process
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"oc-datacenter/conf"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
bookingmodel "cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
@@ -44,13 +46,22 @@ func ClaimName(storageName, executionsID string) string {
|
||||
type PVCSetter struct {
|
||||
ExecutionsID string
|
||||
StorageID string
|
||||
// ClaimSuffix overrides ExecutionsID as the suffix in ClaimName when non-empty.
|
||||
// Used when the PVC namespace differs from the claim name suffix (Admiralty target).
|
||||
ClaimSuffix string
|
||||
}
|
||||
|
||||
func NewPVCSetter(execID, storageID string) *PVCSetter {
|
||||
return &PVCSetter{ExecutionsID: execID, StorageID: storageID}
|
||||
}
|
||||
|
||||
func emitConsiders(executionsID, originID string, provErr error, self bool) {
|
||||
// NewPVCSetterWithClaimSuffix creates a PVCSetter where the claim name suffix
|
||||
// differs from the execution namespace (e.g. Admiralty target provisioning).
|
||||
func NewPVCSetterWithClaimSuffix(storageID, claimSuffix string) *PVCSetter {
|
||||
return &PVCSetter{StorageID: storageID, ClaimSuffix: claimSuffix}
|
||||
}
|
||||
|
||||
func (p *PVCSetter) emitConsiders(executionsID, originID string, provErr error, self bool) {
|
||||
type pvcConsidersPayload struct {
|
||||
OriginID string `json:"origin_id"`
|
||||
ExecutionsID string `json:"executions_id"`
|
||||
@@ -96,7 +107,7 @@ func (p *PVCSetter) InitializeAsSource(ctx context.Context, event PVCProvisionEv
|
||||
sizeStr, err := p.loadStorageSize(event.SourcePeerID)
|
||||
if err != nil {
|
||||
logger.Error().Msg("PVCSetter.InitializeAsSource: " + err.Error())
|
||||
emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
p.emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,19 +117,23 @@ func (p *PVCSetter) InitializeAsSource(ctx context.Context, event PVCProvisionEv
|
||||
)
|
||||
if err != nil {
|
||||
logger.Error().Msg("PVCSetter.InitializeAsSource: failed to create k8s service: " + err.Error())
|
||||
emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
p.emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
return
|
||||
}
|
||||
|
||||
claimName := ClaimName(event.StorageName, event.ExecutionsID)
|
||||
claimSuffix := event.ExecutionsID
|
||||
if p.ClaimSuffix != "" {
|
||||
claimSuffix = p.ClaimSuffix
|
||||
}
|
||||
claimName := ClaimName(event.StorageName, claimSuffix)
|
||||
if err := k.CreatePVC(ctx, claimName, event.ExecutionsID, sizeStr); err != nil {
|
||||
logger.Error().Msg("PVCSetter.InitializeAsSource: failed to create PVC: " + err.Error())
|
||||
emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
p.emitConsiders(event.ExecutionsID, event.OriginID, err, self)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info().Msg("PVCSetter.InitializeAsSource: PVC " + claimName + " created in " + event.ExecutionsID)
|
||||
emitConsiders(event.ExecutionsID, event.OriginID, nil, self)
|
||||
p.emitConsiders(event.ExecutionsID, event.OriginID, nil, self)
|
||||
}
|
||||
|
||||
// TeardownAsSource deletes the PVC from the execution namespace.
|
||||
@@ -172,3 +187,44 @@ func (p *PVCSetter) loadStorageSize(peerID string) (string, error) {
|
||||
}
|
||||
return "10Gi", nil
|
||||
}
|
||||
|
||||
// teardownPVCForExecution deletes all local PVCs provisioned for the execution.
|
||||
// It searches LIVE_STORAGE bookings and resolves the storage name via the live storage.
|
||||
func (p *PVCSetter) TeardownForExecution(ctx context.Context, localPeerID string) {
|
||||
logger := oclib.GetLogger()
|
||||
|
||||
res := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), "", localPeerID, []string{}, nil).
|
||||
Search(&dbs.Filters{
|
||||
And: map[string][]dbs.Filter{
|
||||
"executions_id": {{Operator: dbs.EQUAL.String(), Value: p.ExecutionsID}},
|
||||
"resource_type": {{Operator: dbs.EQUAL.String(), Value: tools.LIVE_STORAGE.EnumIndex()}},
|
||||
},
|
||||
}, "", false)
|
||||
|
||||
if res.Err != "" || len(res.Data) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dbo := range res.Data {
|
||||
b, ok := dbo.(*bookingmodel.Booking)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// Resolve storage name from live storage to compute the claim name.
|
||||
storageName := ResolveStorageName(b.ResourceID, localPeerID)
|
||||
if storageName == "" {
|
||||
continue
|
||||
}
|
||||
logger.Info().Msgf("InfraTeardown: PVC teardown exec=%s storage=%s", p.ExecutionsID, b.ResourceID)
|
||||
event := PVCDeleteEvent{
|
||||
ExecutionsID: p.ExecutionsID,
|
||||
StorageID: b.ResourceID,
|
||||
StorageName: storageName,
|
||||
SourcePeerID: localPeerID,
|
||||
DestPeerID: b.DestPeerID,
|
||||
OriginID: "",
|
||||
}
|
||||
p.StorageID = b.ResourceID
|
||||
p.TeardownAsSource(ctx, event)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user