podspec.go 2.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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 checkpoint

import (
21
	"context"
22
23
24
	"fmt"

	commonconsts "github.com/ai-dynamo/dynamo/deploy/operator/internal/consts"
25
	snapshotprotocol "github.com/ai-dynamo/dynamo/deploy/snapshot/protocol"
26
	corev1 "k8s.io/api/core/v1"
27
	ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
28
29
30
)

func ApplyRestorePodMetadata(labels map[string]string, annotations map[string]string, checkpointInfo *CheckpointInfo) {
31
32
33
34
35
36
37
38
	enabled := checkpointInfo != nil && checkpointInfo.Enabled && checkpointInfo.Ready
	hash := ""
	artifactVersion := ""
	if enabled {
		hash = checkpointInfo.Hash
		artifactVersion = checkpointInfo.ArtifactVersion
	}
	snapshotprotocol.ApplyRestoreTargetMetadata(labels, annotations, enabled, hash, artifactVersion)
39
40
41
}

func InjectCheckpointIntoPodSpec(
42
43
44
	ctx context.Context,
	reader ctrlclient.Reader,
	namespace string,
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	podSpec *corev1.PodSpec,
	checkpointInfo *CheckpointInfo,
) error {
	if checkpointInfo == nil || !checkpointInfo.Enabled {
		return nil
	}

	info := checkpointInfo
	if info.Hash == "" {
		if info.Identity == nil {
			return fmt.Errorf("checkpoint enabled but identity is nil and hash is not set")
		}

		hash, err := ComputeIdentityHash(*info.Identity)
		if err != nil {
			return fmt.Errorf("failed to compute identity hash: %w", err)
		}
		info.Hash = hash
	}

65
66
67
	if len(podSpec.Containers) == 0 {
		return fmt.Errorf("no container found to inject checkpoint config")
	}
68
69
70
71
72
73
74
75
	var mainContainer *corev1.Container
	for i := range podSpec.Containers {
		if podSpec.Containers[i].Name == commonconsts.MainContainerName {
			mainContainer = &podSpec.Containers[i]
			break
		}
	}
	if mainContainer == nil {
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		return fmt.Errorf("main container not found in pod spec")
	}
	if reader == nil {
		return fmt.Errorf("checkpoint client is required")
	}
	if err := snapshotprotocol.PrepareRestorePodSpecForCheckpoint(
		ctx,
		reader,
		namespace,
		podSpec,
		mainContainer,
		info.Hash,
		info.ArtifactVersion,
		commonconsts.SeccompProfilePath,
		info.Ready,
	); err != nil {
92
93
94
		return err
	}

95
96
	EnsurePodInfoVolume(podSpec)
	EnsurePodInfoMount(mainContainer)
97
98
	return nil
}