Skip to content

Commit

Permalink
fix: k8s resource config overriden by defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rvql authored and sharang committed Dec 19, 2024
1 parent 3388cc7 commit 0e881db
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 53 deletions.
27 changes: 14 additions & 13 deletions agent/src/platform/kubernetes/api_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ use tokio::{runtime::Runtime, task::JoinHandle};

use super::resource_watcher::{
default_resources, supported_resources, GenericResourceWatcher, GroupVersion, Resource,
Watcher, WatcherConfig,
ResourceWatcherFactory, SelectedGv, Watcher, WatcherConfig,
};
use crate::{
config::{handler::PlatformAccess, ApiResources},
error::{Error, Result},
exception::ExceptionHandler,
platform::kubernetes::resource_watcher::ResourceWatcherFactory,
rpc::Session,
trident::AgentId,
utils::{
Expand Down Expand Up @@ -338,7 +337,7 @@ impl ApiWatcher {
continue;
};
resources.push(Resource {
selected_gv: Some(sr.group_versions[index]),
selected_gv: SelectedGv::Specified(sr.group_versions[index]),
field_selector: r.field_selector.clone(),
..sr.clone()
});
Expand Down Expand Up @@ -372,7 +371,7 @@ impl ApiWatcher {
"found {} api in group core/{}",
api_resource.name, core_version
);
resources[index].selected_gv = Some(GroupVersion {
resources[index].selected_gv = SelectedGv::Inferred(GroupVersion {
group: "core",
version: core_version,
});
Expand Down Expand Up @@ -457,23 +456,25 @@ impl ApiWatcher {
"found {} api in group {}",
resource_name, version.group_version
);
if resource.selected_gv.is_none() {
resource.selected_gv = Some(*gv);
} else {
let selected = &resource.selected_gv.as_ref().unwrap();
if &gv != selected {
match &resource.selected_gv {
SelectedGv::None => {
resource.selected_gv = SelectedGv::Inferred(*gv)
}
SelectedGv::Inferred(selected) if gv != selected => {
// must exist
let prev_index = resource
.group_versions
.iter()
.position(|g| &g == selected)
.position(|g| g == selected)
.unwrap();
// prior
if gv_index < prev_index {
debug!("use more suitable {} api in {}", resource_name, gv);
resource.selected_gv = Some(*gv);
resource.selected_gv = SelectedGv::Inferred(*gv);
}
}
// do nothing if a group version is specified in agent config
_ => (),
}
}
}
Expand All @@ -491,7 +492,7 @@ impl ApiWatcher {
for r in resources.iter_mut() {
if r.selected_gv.is_none() {
warn!("resource {} not found, use defaults", r.name);
r.selected_gv = Some(r.group_versions[0]);
r.selected_gv = SelectedGv::Inferred(r.group_versions[0]);
}
}

Expand Down Expand Up @@ -554,7 +555,7 @@ impl ApiWatcher {
for r in resources {
let key = WatcherKey {
name: r.name,
group: r.selected_gv.as_ref().unwrap().group,
group: r.selected_gv.unwrap().group,
};
if let Some(watcher) =
watcher_factory.new_watcher(r, namespace, stats_collector, watcher_config)
Expand Down
86 changes: 54 additions & 32 deletions agent/src/platform/kubernetes/resource_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,44 @@ impl fmt::Display for GroupVersion {
}
}

#[derive(Clone, Debug)]
pub(crate) enum SelectedGv {
None,
Specified(GroupVersion),
Inferred(GroupVersion),
}

impl SelectedGv {
pub fn is_none(&self) -> bool {
matches!(self, SelectedGv::None)
}

pub fn unwrap(&self) -> &GroupVersion {
match self {
SelectedGv::None => unreachable!(),
SelectedGv::Specified(gv) => gv,
SelectedGv::Inferred(gv) => gv,
}
}
}

#[derive(Clone, Debug)]
pub struct Resource {
pub name: &'static str,
pub pb_name: &'static str,
// supported group versions ordered by priority
pub group_versions: Vec<GroupVersion>,
// group version to use
pub selected_gv: Option<GroupVersion>,
pub selected_gv: SelectedGv,
pub field_selector: String,
}

impl fmt::Display for Resource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.selected_gv {
Some(gv) => write!(f, "{}/{}", gv, self.name),
None => write!(f, "{}: {:?}", self.name, self.group_versions),
SelectedGv::None => write!(f, "{}: {:?}", self.name, self.group_versions),
SelectedGv::Specified(gv) => write!(f, "{}/{}", gv, self.name),
SelectedGv::Inferred(gv) => write!(f, "{}/{}", gv, self.name),
}
}
}
Expand All @@ -263,7 +285,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -273,7 +295,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -283,7 +305,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -293,7 +315,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -303,7 +325,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -313,7 +335,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -323,7 +345,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -333,7 +355,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -343,7 +365,7 @@ pub fn default_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -363,7 +385,7 @@ pub fn default_resources() -> Vec<Resource> {
version: "v1beta1",
},
],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
]
Expand All @@ -378,7 +400,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -388,7 +410,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -398,7 +420,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -408,7 +430,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -418,7 +440,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "core",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -428,7 +450,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -438,7 +460,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -448,7 +470,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "apps",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -468,7 +490,7 @@ pub fn supported_resources() -> Vec<Resource> {
version: "v1alpha1",
},
],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -488,7 +510,7 @@ pub fn supported_resources() -> Vec<Resource> {
version: "v1beta1",
},
],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -498,7 +520,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "route.openshift.io",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -508,7 +530,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "crd.pingan.org",
version: "v1alpha1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -518,7 +540,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "apps.kruise.io",
version: "v1alpha1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -528,7 +550,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "crd.projectcalico.org",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
Resource {
Expand All @@ -538,7 +560,7 @@ pub fn supported_resources() -> Vec<Resource> {
group: "opengauss.cmbc.com.cn",
version: "v1",
}],
selected_gv: None,
selected_gv: SelectedGv::None,
field_selector: String::new(),
},
]
Expand Down Expand Up @@ -1443,7 +1465,7 @@ impl ResourceWatcherFactory {
namespace,
config,
)),
"statefulsets" => match resource.selected_gv.as_ref().unwrap() {
"statefulsets" => match resource.selected_gv.unwrap() {
GroupVersion {
group: "apps.kruise.io",
version: "v1beta1",
Expand Down Expand Up @@ -1475,7 +1497,7 @@ impl ResourceWatcherFactory {
warn!(
"unsupported resource {} group version {}",
resource.name,
resource.selected_gv.as_ref().unwrap()
resource.selected_gv.unwrap()
);
return None;
}
Expand All @@ -1495,7 +1517,7 @@ impl ResourceWatcherFactory {
namespace,
config,
)),
"ingresses" => match resource.selected_gv.as_ref().unwrap() {
"ingresses" => match resource.selected_gv.unwrap() {
GroupVersion {
group: "networking.k8s.io",
version: "v1",
Expand Down Expand Up @@ -1527,7 +1549,7 @@ impl ResourceWatcherFactory {
warn!(
"unsupported resource {} group version {}",
resource.name,
resource.selected_gv.as_ref().unwrap()
resource.selected_gv.unwrap()
);
return None;
}
Expand Down
Loading

0 comments on commit 0e881db

Please sign in to comment.