-
Notifications
You must be signed in to change notification settings - Fork 501
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
Orphan pods cleaner #201
Merged
Merged
Orphan pods cleaner #201
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
805fcef
add ophan pods cleaner
weekface 9576d9f
Merge branch 'master' into orphan-pods-cleaner
weekface 2553379
upgrade duration to 10 minutes
weekface e666056
Merge branch 'master' into orphan-pods-cleaner
weekface 5cf9151
tiny fix
weekface eb537c8
Merge branch 'orphan-pods-cleaner' of https://github.com/weekface/tid…
weekface 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
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,109 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package member | ||
|
||
import ( | ||
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1" | ||
"github.com/pingcap/tidb-operator/pkg/controller" | ||
"github.com/pingcap/tidb-operator/pkg/label" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
corelisters "k8s.io/client-go/listers/core/v1" | ||
) | ||
|
||
const ( | ||
skipReasonOrphanPodsCleanerIsNotPDOrTiKV = "orphan pods cleaner: member type is not pd or tikv" | ||
skipReasonOrphanPodsCleanerPVCNameIsEmpty = "orphan pods cleaner: pvcName is empty" | ||
skipReasonOrphanPodsCleanerPVCIsFound = "orphan pods cleaner: pvc is found" | ||
) | ||
|
||
// OrphanPodsCleaner implements the logic for cleaning the orphan pods(has no pvc) | ||
type OrphanPodsCleaner interface { | ||
Clean(*v1alpha1.TidbCluster) (map[string]string, error) | ||
} | ||
|
||
type orphanPodsCleaner struct { | ||
podLister corelisters.PodLister | ||
podControl controller.PodControlInterface | ||
pvcLister corelisters.PersistentVolumeClaimLister | ||
} | ||
|
||
// NewOrphanPodsCleaner returns a OrphanPodsCleaner | ||
func NewOrphanPodsCleaner(podLister corelisters.PodLister, | ||
podControl controller.PodControlInterface, | ||
pvcLister corelisters.PersistentVolumeClaimLister) OrphanPodsCleaner { | ||
return &orphanPodsCleaner{podLister, podControl, pvcLister} | ||
} | ||
|
||
func (opc *orphanPodsCleaner) Clean(tc *v1alpha1.TidbCluster) (map[string]string, error) { | ||
ns := tc.GetNamespace() | ||
// for unit test | ||
skipReason := map[string]string{} | ||
|
||
selector, err := label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).Selector() | ||
if err != nil { | ||
return skipReason, err | ||
} | ||
pods, err := opc.podLister.Pods(ns).List(selector) | ||
if err != nil { | ||
return skipReason, err | ||
} | ||
|
||
for _, pod := range pods { | ||
podName := pod.GetName() | ||
l := label.Label(pod.Labels) | ||
if !(l.IsPD() || l.IsTiKV()) { | ||
skipReason[podName] = skipReasonOrphanPodsCleanerIsNotPDOrTiKV | ||
continue | ||
} | ||
|
||
var pvcName string | ||
for _, vol := range pod.Spec.Volumes { | ||
if vol.PersistentVolumeClaim != nil { | ||
pvcName = vol.PersistentVolumeClaim.ClaimName | ||
break | ||
} | ||
} | ||
if pvcName == "" { | ||
skipReason[podName] = skipReasonOrphanPodsCleanerPVCNameIsEmpty | ||
continue | ||
} | ||
|
||
_, err := opc.pvcLister.PersistentVolumeClaims(ns).Get(pvcName) | ||
if err == nil { | ||
skipReason[podName] = skipReasonOrphanPodsCleanerPVCIsFound | ||
continue | ||
} | ||
if !errors.IsNotFound(err) { | ||
return skipReason, err | ||
} | ||
|
||
err = opc.podControl.DeletePod(tc, pod) | ||
if err != nil { | ||
return skipReason, err | ||
} | ||
} | ||
|
||
return skipReason, nil | ||
} | ||
|
||
type fakeOrphanPodsCleaner struct{} | ||
|
||
// NewFakeOrphanPodsCleaner returns a fake orphan pods cleaner | ||
func NewFakeOrphanPodsCleaner() OrphanPodsCleaner { | ||
return &fakeOrphanPodsCleaner{} | ||
} | ||
|
||
func (fopc *fakeOrphanPodsCleaner) Clean(_ *v1alpha1.TidbCluster) (map[string]string, error) { | ||
return nil, nil | ||
} |
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.
why not use
continue
here?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.
If there is an error, return directly.
Is this reasonable?