-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable BPF without disruption #3183
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2019-2024 Tigera, Inc. All rights reserved. | ||
|
||
// 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 installation | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"strconv" | ||
|
||
"github.com/tigera/operator/pkg/controller/utils" | ||
|
||
crdv1 "github.com/tigera/operator/pkg/apis/crd.projectcalico.org/v1" | ||
"github.com/tigera/operator/pkg/common" | ||
"github.com/tigera/operator/pkg/render" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// bpfValidateAnnotations validate Felix Configuration annotations match BPF Enabled spec for all scenarios. | ||
func bpfValidateAnnotations(fc *crdv1.FelixConfiguration) error { | ||
var annotationValue *bool | ||
if fc.Annotations[render.BPFOperatorAnnotation] != "" { | ||
v, err := strconv.ParseBool(fc.Annotations[render.BPFOperatorAnnotation]) | ||
annotationValue = &v | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// The values are considered matching if one of the following is true: | ||
// - Both values are nil | ||
// - Neither are nil and they have the same value. | ||
// Otherwise, the we consider the annotation to not match the spec field. | ||
match := annotationValue == nil && fc.Spec.BPFEnabled == nil | ||
match = match || annotationValue != nil && fc.Spec.BPFEnabled != nil && *annotationValue == *fc.Spec.BPFEnabled | ||
|
||
if !match { | ||
return errors.New(`Unable to set bpfEnabled: FelixConfiguration "default" has been modified by someone else, refusing to override potential user configuration.`) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// isRolloutCompleteWithBPFVolumes checks if the calico-node DaemonSet | ||
// rollout process is completed with BPF volume mount been created. | ||
// If the Installation resource has been patched to dataplane: BPF then the | ||
// calico-node daemonset will be re-created with BPF infrastructure such as | ||
// the "bpffs" volumne mount etc. which will cause the DS to do a rolling update. | ||
// Therefore, one way to check that the daemonset rolling update is complete is | ||
// to compare the DS status current scheduled pods equals the updated number and | ||
// the current scheduled pods also equals the number available. When all these | ||
// checks are reconciled then FelixConfig can be patched as bpfEnabled: true. | ||
func isRolloutCompleteWithBPFVolumes(ds *appsv1.DaemonSet) bool { | ||
for _, volume := range ds.Spec.Template.Spec.Volumes { | ||
if volume.Name == render.BPFVolumeName { | ||
//return ds.Status.CurrentNumberScheduled == ds.Status.UpdatedNumberScheduled && ds.Status.CurrentNumberScheduled == ds.Status.NumberAvailable | ||
if ds.Status.CurrentNumberScheduled == ds.Status.UpdatedNumberScheduled && ds.Status.CurrentNumberScheduled == ds.Status.NumberAvailable { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func setBPFEnabledOnFelixConfiguration(fc *crdv1.FelixConfiguration, bpfEnabled bool) error { | ||
err := bpfValidateAnnotations(fc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
text := strconv.FormatBool(bpfEnabled) | ||
|
||
// Add an annotation matching the field value. This allows the operator to compare the annotation to the field | ||
// when performing an update to determine if another entity has modified the value since the last write. | ||
var fcAnnotations map[string]string | ||
if fc.Annotations == nil { | ||
fcAnnotations = make(map[string]string) | ||
} else { | ||
fcAnnotations = fc.Annotations | ||
} | ||
fcAnnotations[render.BPFOperatorAnnotation] = text | ||
fc.SetAnnotations(fcAnnotations) | ||
fc.Spec.BPFEnabled = &bpfEnabled | ||
return nil | ||
} | ||
|
||
func bpfEnabledOnDaemonsetWithEnvVar(ds *appsv1.DaemonSet) (bool, error) { | ||
bpfEnabledStatus := false | ||
var err error | ||
|
||
if ds != nil && | ||
!reflect.DeepEqual(ds.Spec, appsv1.DaemonSetSpec{}) && | ||
!reflect.DeepEqual(ds.Spec.Template, corev1.PodTemplateSpec{}) && | ||
!reflect.DeepEqual(ds.Spec.Template.Spec, corev1.PodSpec{}) { | ||
bpfEnabledEnvVar := utils.GetPodEnvVar(ds.Spec.Template.Spec, common.NodeDaemonSetName, "FELIX_BPFENABLED") | ||
if bpfEnabledEnvVar != nil { | ||
bpfEnabledStatus, err = strconv.ParseBool(*bpfEnabledEnvVar) | ||
} | ||
} | ||
|
||
return bpfEnabledStatus, err | ||
} | ||
|
||
func bpfEnabledOnFelixConfig(fc *crdv1.FelixConfiguration) bool { | ||
return fc.Spec.BPFEnabled != nil && *fc.Spec.BPFEnabled | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nit, but the function comments should start with the function name. e.g.,