Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Apr 30, 2023
1 parent 997b04a commit 1fafa7b
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 39 deletions.
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ import (
operator = crd + controller + webhook
*/



func main() {

logf.SetLogger(zap.New())
// 1. 管理器初始化
mgr, err := manager.New(k8sconfig.K8sRestConfig(), manager.Options{
Logger: logf.Log.WithName("poddeployer-operator"),
Logger: logf.Log.WithName("poddeployer-operator"),
})
if err != nil {
mgr.GetLogger().Error(err, "unable to set up manager")
Expand All @@ -46,7 +44,6 @@ func main() {
os.Exit(1)
}


// 3. 控制器相关
podReStarterCtl := controller.NewPodDeployerController()

Expand All @@ -59,23 +56,18 @@ func main() {
).
Complete(podReStarterCtl)


errC := make(chan error)

// 4. 启动controller管理器
go func() {
klog.Info("controller start!! ")
if err = mgr.Start(signals.SetupSignalHandler()); err != nil {
errC <-err
errC <- err
}
}()



// 这里会阻塞
getError := <-errC
log.Println(getError.Error())

}


1 change: 0 additions & 1 deletion pkg/apis/podDeployer/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Package v1alpha1 contains API Schema definitions for the ecs v1alpha1 API group
// +k8s:deepcopy-gen=package,register
// +groupName=api.practice.com
Expand Down
7 changes: 4 additions & 3 deletions pkg/apis/podDeployer/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
)

const (
PodDeployerGroup = "api.practice.com"
PodDeployerVersion = "v1alpha1"
PodDeployerGroup = "api.practice.com"
PodDeployerVersion = "v1alpha1"
PodDeployerApiVersion = "api.practice.com/v1alpha1"
PodDeployerKind = "Poddeployer"
PodDeployerKind = "Poddeployer"
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: PodDeployerGroup, Version: PodDeployerVersion}

Expand Down
12 changes: 4 additions & 8 deletions pkg/apis/podDeployer/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ type Poddeployer struct {
Spec PodDeployerSpec `json:"spec,omitempty"`
}


type PodDeployerSpec struct {
DeploymentSpec appsv1.DeploymentSpec `json:"deployment_spec"`
PriorityImages []PriorityImage `json:"priority_images,omitempty"`
DeploymentSpec appsv1.DeploymentSpec `json:"deployment_spec"`
PriorityImages []PriorityImage `json:"priority_images,omitempty"`
}

type PriorityImage struct {
Image string `json:"image"`
Value int `json:"value"`
Image string `json:"image"`
Value int `json:"value"`
}


// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// PodReStarterList
Expand All @@ -39,5 +37,3 @@ type PoddeployerList struct {

Items []Poddeployer `json:"items"`
}


4 changes: 1 addition & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var wg = sync.WaitGroup{}

type PodDeployerController struct {
client.Client

}

func NewPodDeployerController() *PodDeployerController {
Expand Down Expand Up @@ -66,7 +65,7 @@ func (r *PodDeployerController) Reconcile(ctx context.Context, req reconcile.Req
}

// InjectClient 使用controller-runtime 需要注入的client
func(r *PodDeployerController) InjectClient(c client.Client) error {
func (r *PodDeployerController) InjectClient(c client.Client) error {
r.Client = c
return nil
}
Expand All @@ -82,4 +81,3 @@ func (r *PodDeployerController) DeploymentDeleteHandler(event event.DeleteEvent,
}
}
}

8 changes: 3 additions & 5 deletions pkg/controller/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func (r *PodDeployerController) handleDeployment(ctx context.Context, podDeploye
return nil
}


func mutateDeployment(podDeployer *podrestarterv1alpha1.Poddeployer, deployment *appsv1.Deployment) {
deployment.Spec = podDeployer.Spec.DeploymentSpec
labels := map[string]string{
Expand All @@ -94,7 +93,7 @@ func calculatePriorityImages(podDeployer *podrestarterv1alpha1.Poddeployer) []po
}

// 替换image,并返回剩下排序后的images
func handleDeploymentImageSort(priorityImages []podrestarterv1alpha1.PriorityImage, podDeployer *podrestarterv1alpha1.Poddeployer) ([]v1.Container, error){
func handleDeploymentImageSort(priorityImages []podrestarterv1alpha1.PriorityImage, podDeployer *podrestarterv1alpha1.Poddeployer) ([]v1.Container, error) {
if len(priorityImages) != len(podDeployer.Spec.DeploymentSpec.Template.Spec.Containers) {
return nil, errors.New("priorityImage len error")
}
Expand Down Expand Up @@ -127,8 +126,8 @@ func patchDeployment(deploymentName, namespace string, container *v1.Container)

pa := make([]patchOperation, 0)
p := patchOperation{
Op: "add",
Path: fmt.Sprintf("/spec/template/spec/containers/-"),
Op: "add",
Path: fmt.Sprintf("/spec/template/spec/containers/-"),
Value: container,
}
pa = append(pa, p)
Expand All @@ -138,7 +137,6 @@ func patchDeployment(deploymentName, namespace string, container *v1.Container)
return
}


jsonPatch, err := jsonpatch.DecodePatch(patchBytes)
if err != nil {
klog.Error("DecodePatch error: ", err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/k8sconfig/init_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
)


// InitClient 初始化k8s-client
func InitClient(config *rest.Config) kubernetes.Interface {
c, err := kubernetes.NewForConfig(config)
Expand All @@ -20,4 +19,4 @@ var ClientSet kubernetes.Interface

func init() {
ClientSet = InitClient(K8sRestConfig())
}
}
5 changes: 2 additions & 3 deletions pkg/k8sconfig/init_k8s_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func K8sRestConfig() *rest.Config {
}

path := common.GetWd()
config, err := clientcmd.BuildConfigFromFlags("", path + "/resources/config")
config, err := clientcmd.BuildConfigFromFlags("", path+"/resources/config")
if err != nil {
log.Fatal(err)
}
Expand All @@ -29,12 +29,11 @@ func K8sRestConfig() *rest.Config {
}

// k8sRestConfigInPod 集群内部POD里使用
func k8sRestConfigInPod() *rest.Config{
func k8sRestConfigInPod() *rest.Config {

config, err := rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
return config
}

1 change: 0 additions & 1 deletion pkg/util/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ func getRsIdsByDeployment(dep *appv1.Deployment, clientSet kubernetes.Interface)
}
return ret
}

3 changes: 0 additions & 3 deletions test/test_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ func main() {
fmt.Println(ex)

}



0 comments on commit 1fafa7b

Please sign in to comment.